Add row to databound Datagridview.

P

Priyal

Hi.

I got one windows application where i got datagridview and i am filling my
datagridview via dataset.

DataGridView1.DataSource = trxdataset.Tables(0)

Now as per my requirement i need to add rows dynamically into the
datagridview. This row should add at the end of the rows which are already
present in datagridview.

Can anybody tell me how do i do that?

Regards

Priyal
 
M

Morten Wennevik [C# MVP]

Hi,

You need to add the rows to the DataSource, in this case, the DataTable.
The code sample below demonstrates how you can add new rows to a DataTable
bound to a DataGridView. Click the button to add new rows.


DataGridView dgv = new DataGridView();
protected override void OnLoad(EventArgs e)
{
Controls.Add(dgv);
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));

DataRow dr = dt.NewRow();
dr[0] = "Hello";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = "World";
dt.Rows.Add(dr);

dgv.DataSource = dt;

Button bt = new Button();
bt.Location = new Point(0, dgv.Bottom + 5);
bt.Click += new EventHandler(bt_Click);
Controls.Add(bt);
}

void bt_Click(object sender, EventArgs e)
{
DataTable dt = dgv.DataSource as DataTable;
DataRow dr = dt.NewRow();
dr[0] = "Bye";
dt.Rows.Add(dr);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top