Click here to Skip to main content
16,015,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

Am working on winform. I have one Tab control and inside the tab control I added one DataGridView. Am binding the data to DataGridView from code. I want to check the DataGridViewCheckboxCell to Checked when loading... Here is the code
C#
private void Form1_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("name");

    DataRow dr;
    dr = dt.NewRow();
    dr["name"] = "Rajesh";

    dt.Rows.Add(dr);
    dataGridView1.DataSource = dt;

    
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
    }
  
}

But it should not be checking the checkbox cell inside the Tabcontrol. Please tell me the solution.
Posted
Updated 16-Mar-12 18:09pm
v4
Comments
ProEnggSoft 17-Mar-12 0:10am    
Edit: pre tag for C# code, code tags added and capitalization correction - PES

1 solution

change ur code to...
C#
DataTable dt = new DataTable();
dt.Columns.Add("name");

DataRow dr;
dr = dt.NewRow();
dr["name"] = "Rajesh";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["name"] = "Ratnesh";
dt.Rows.Add(dr);

DataGridViewRow dr1;
for (int i = 0; i < dt.Rows.Count; i++)
{
    dr1 = new DataGridViewRow();
    dr1.CreateCells(dataGridView1);
    dr1.Cells[0].Value = dt.Rows[i]["name"];
    dr1.Cells[1].Value = true;
    dataGridView1.Rows.Add(dr1);
}
////or u may direct add row in grid view

DataGridViewRow dr1;
dr1 = new DataGridViewRow();
dr1.CreateCells(dataGridView1);
dr1.Cells[0].Value = "Rajesh";
dr1.Cells[1].Value = true;
dataGridView1.Rows.Add(dr1);
 
Share this answer
 
v3
Comments
ProEnggSoft 17-Mar-12 8:05am    
Edit: pre tag for C# code added - PES

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900