9

I've inserted a checkbox column and textbox column on the datagridview. how can add rows manually on the textbox column.

It should be like this:

checkbox | textbox
............................
checkbox | item1
checkbox | item2
checkbox | item3
checkbox | item4

Here is the code for checkbox and textbox on datagridview

public void loadgrid()
{
    DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
    CheckBox chk = new CheckBox();
    checkboxColumn.Width = 25;
    dataGridView1.Columns.Add(checkboxColumn);

    DataGridViewTextBoxColumn textboxcolumn = new DataGridViewTextBoxColumn();
    TextBox txt = new TextBox();
    textboxcolumn.Width = 150;
    dataGridView1.Columns.Add(textboxcolumn);     
}
1
  • Your references to chk and txt objects aren't doing anything in this code.
    – LarsTech
    Commented Nov 13, 2012 at 15:05

2 Answers 2

16

You can pass an object array that contains the values which should be inserted into the DataGridView in the order how the columns are added. For instance you could use this:

dataGridView1.Rows.Add(new object[] { true, "string1" });
dataGridView1.Rows.Add(new object[] { false, "string2" });

And you can build object array from whatever you want, just be sure to match the type (i.e. use bool for checkedColumn)

0
7

You can use the Rows collection to manually populate a DataGridView control instead of binding it to a data source.

this.dataGridView1.Rows.Add("five", "six", "seven", "eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");

Take a look at the documentation

And you can do something like this too:

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["Column2"].Value = "XYZ";
row.Cells["Column6"].Value = 50.2;
yourDataGridView.Rows.Add(row);

See this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.