1

I am using Windows form and C# and MySQL.

In that i add three unbound columns in Datagridview with the help of control tag witch is in Datagridview right top.

column1 is DataGridViewCheckBoxColumn. column 2 and 3 is DataGridViewTextBoxColumn.

After that i wrote code to fill the Datagrid view.

connection.Open();
                 DataTable dt = new DataTable();
                 // Create a new data adapter based on the specified query.
                 MySqlDataAdapter da = new MySqlDataAdapter("select admin_no,name from " + FormName + " where class_code='" + class_code + "' and division_code='" + division_code + "' and Delete_Status = 0", connection);
                 //SQl command builder is used to get data from database based on query
                 MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);
                 //Fill data table
                 da.Fill(dt);
                 //assigning data table to Datagridview
                 dataGridView1.DataSource = dt;
                 //Resize the Datagridview column to fit the gridview columns with data in datagridview
                 dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
                 connection.Close();

In That I got

enter image description here

Here u see the two columns of Textbox is empty. and other two is added.

I don't want to remove the textbox. So I want to populate the data with in my textbox, not a auto generated one.,..

Please help me, to solve this problem....

Thanks, I got the value in template Textbox columns. But the Last two columns of auto generated columns are added. How to remove that.

connection.Open();
                 DataTable dt = new DataTable();                 
                 MySqlDataAdapter da = new MySqlDataAdapter("select admin_no,name from " + FormName + " where class_code='" + class_code + "' and division_code='" + division_code + "' and Delete_Status = 0", connection);                 
                 MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);                 
                 da.Fill(dt);                 
                 dataGridView1.DataSource = dt;                                 
                 dataGridView1.Columns[1].DataPropertyName = "admin_no";
                 dataGridView1.Columns[2].DataPropertyName = "name";
                 connection.Close();

enter image description here

Help Me again Please.

Sorry i forgot "dataGridView1.AutoGenerateColumns = false;".

Thanks I got my requirement.

2 Answers 2

2

You have to set DataGridViewTextBoxColumn.DataPropertyName="name_of_field". Take a look at MSDN article.

PS: You should have to use Parameterized query instead of hard-coded sql string to prevent sql-injection.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. But where Can i add this code after "dataGridView1.DataSource = dt;" or before..
@Sagotharan - I think you've added columns via Columns property(wizard) of dataGridview control in designer. So open Columns property and set DataPropertyName property.
Thanks, I got the text box values. But the last two columns was also included. How can i remove.
@Sagotharan - You have to turn off the autogen columns. dataGridView1.AutoGenerateColumns = false;
2

Go to your datagridview properties.

Search for Columns.. On your unbound Admin No Column set its DataproperyName to admin_no

This will bind your DataTable Column admin_no to datagridview unbound Admin No column automatically. Do this also on your other unbound columns.

Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.