Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding rows on datagridview manually

Tags:

c#

winforms

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);     
}
like image 996
user1647667 Avatar asked Nov 13 '12 14:11

user1647667


People also ask

How do you fix rows Cannot be programmatically added to the DataGridView rows collection when the control is data bound?

You have two solutions: Either you strip off the data binding and do the boilerplate code yourself. Or you add a new record to the datasource and refresh the gridview => it will update itself with the new row.

How do you add columns in data grid?

To add a column using the designer ) on the upper-right corner of the DataGridView control, and then select Add Column. In the Add Column dialog box, choose the Databound Column option and select a column from the data source, or choose the Unbound Column option and define the column using the fields provided.

What is the difference between DataGrid and DataGridView?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.


1 Answers

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

like image 169
CloudyMarble Avatar answered Oct 02 '22 04:10

CloudyMarble