How to insert a new row into datagridview(bound to database)
-
Sunday, May 06, 2012 2:20 AM
There is a link for inserting new record on datagridview. http://www.mindstick.com/Articles/4c22202b-c1e7-47f2-a1f8-5ac72662ee91/?Insert,%20Update,%20Delete%20in%20DataGridView
So we can fill in the text into textboxs and click the button. But my question is that if the gridview is populated like http://pedromj.dyndns.org/publico/DataGridViewWindows01.png. Can we pass the parameters from the last blank row cells to datasource? Thus we can make the the interface nice and neat.
Need code(C#) assistance.
Thank you very much.
All Replies
-
Sunday, May 06, 2012 3:28 AM
Hi:)
Yes I think so——If you bind the DataTable generated from a SqlDataAdapter to the DataGridView,and once you've added rows to the GridView,it will be also mapped to the real DataTable。But you should drag and drop a button and click to write the modified things into real table by using:
SqlDataAdapter.Update(DataTable instance);
- Edited by ProgrammingVolunteerMVP Sunday, May 06, 2012 3:28 AM
-
Sunday, May 06, 2012 7:13 PM
How to get the last row's cells?
Row[x][y]....
x=DataGridView1.Rows.Count-1?
-
Monday, May 07, 2012 1:32 AM
How to get the last row's cells?
Row[x][y]....
x=DataGridView1.Rows.Count-1?
Yes, I think you can do this:
dataGridView1.Rows[DataGridView1.Rows.Count-1].Cells[Column Index].Value;
Maybe the value is null (Nothing in Basic), and the row isn't mapped to the real DataTable as its DataSource of DataGridView.
In my mind,I suggest you set AllowUserToAdd=false,this means you cannot directly add data record through the DataGridView directly,and then you can use this.
-
Monday, May 07, 2012 5:13 AMModerator
The last row is:
this.dataGridView1.Rows[this.dataGridView1.Rows.Count-2].Cells[0].Value
And the new row is:
this.dataGridView1.Rows[this.dataGridView1.Rows.Count-1].Cells[0].Value
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Tuesday, May 08, 2012 4:15 AM
-
Monday, May 07, 2012 12:46 PM
It looks great. Can we use a loop to go through al cells?
Right now I use
DataRow dr = new DataRow(); dr["column1"] = this.dataGridView1.Rows[this.dataGridView1.Rows.Count-2].Cells[0].Value; \\ can I use dr[0] something so we can use foreach..
-
Monday, May 07, 2012 1:53 PM
ure:
//define datatable (with all columns you want to have)... //then: DataRow dr; for(int i=0;i<dataGridView1.Rows.Count; i++) { dr = table.NewRow(); dr[i] = dataGridView1[0, i].Value; dataTable.Rows.Add(dr); }
Mitja
- Edited by Mitja BoncaMicrosoft Community Contributor Monday, May 07, 2012 1:54 PM
- Marked As Answer by ardmore Monday, May 07, 2012 2:16 PM
- Unmarked As Answer by ardmore Monday, May 07, 2012 2:17 PM
-
Monday, May 07, 2012 2:19 PM
Enen. But I found it might befor(int i=0;i<dataGridView1.Columns.Count; i++)
-
Monday, May 07, 2012 2:30 PM
Depends. If you wanna loop Rows or Columns? Decide it by your self. I actually dont know, I cant read your mind.Mitja
- Marked As Answer by ardmore Monday, May 07, 2012 2:39 PM



