Add new row to datagridview one by one dynamically
-
2009년 9월 18일 금요일 오후 11:25
Hi Guys,
I have problem in adding new row one by one to datagridview when the user clicks on add button in runtime. When the user clicks on the add button, the data row in left gridview will be displayed in the right hand gridview. I have the following code already, however, it only works for one new row, every time the button is clicked, the row will be replaced by a new row. Is there any way I can add new row to the existing rows by clicking button? Please suggests any solution. Much appreciated.
Cheers
Chris
DataTable dt = new DataTable(); dt.Columns.Add("Object_ID"); dt.Columns.Add("ThumbnailImage"); dt.Rows.Add(Convert.ToInt32(dgvObjects.CurrentRow.Cells[1].Value), dgvObjects.CurrentRow.Cells[5].Value.ToString()); dgvCarousel.DataSource = dt;
모든 응답
-
2009년 9월 19일 토요일 오전 12:17
Since you already have a DataTable populated in the DataSource, you can do this:
private void button3_Click(object sender, EventArgs e)
{
(dgvCarousel.DataSource as DataTable).Rows.Add(....add row....);
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- 답변으로 제안됨 paras kumarMicrosoft Employee 2009년 9월 19일 토요일 오전 2:50
- 답변으로 제안 취소됨 Royal Oak 2009년 9월 19일 토요일 오전 3:51
-
2009년 9월 19일 토요일 오전 12:23
The reason it works for one row because I am guessing if this is your code you are creating a new instance of DataTable each time.
Create the DataTable in the constructor. Then populate it in your button event or however you are doing it.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- 답변으로 표시됨 Royal Oak 2009년 9월 19일 토요일 오전 3:51
-
2009년 9월 19일 토요일 오전 3:06
Hi John,
Thanks very much for your reply. I tries your way.
the code looks like this, is this right? I kind of guessing it wrong as it didnt use dt object.DataTable dt = new DataTable(); dt.Columns.Add("Object_ID"); dt.Columns.Add("ThumbnailImage"); (dgvCarousel.DataSource as DataTable).Rows.Add(Convert.ToInt32(dgvObjects.CurrentRow.Cells[1].Value), dgvObjects.CurrentRow.Cells[5].Value.ToString());For your second post, u suggests creating the DataTable in the constructor, then populate it in the button event. I still not sure how to implement this, could u pls give me more hints on this. I try sth like this according to your saying:
outside the button click:
DataTable dt = new DataTable();
bool exist = false;
inside the button click:
private void btnAddObject_Click(object sender, EventArgs e)
{
if (exist == false)
{
dt.Columns.Add("Object_ID");
dt.Columns.Add("ThumbnailImage");
exist = true;
}
int object_id = Convert.ToInt32(dgvObjects.CurrentRow.Cells[1].Value);
bool objectExist = false;
if (dgvCarousel.Rows.Count > 0)
{
for (int i = 0; i < dgvCarousel.Rows.Count; i++)
{
if (dgvObjects.CurrentRow.Cells[1].Value.ToString() == dgvCarousel.Rows[i].Cells[0].Value.ToString())
{
MessageBox.Show("This item has been added already.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
objectExist = true;
break;
}
}
if (objectExist == false)
{
dt.Rows.Add(Convert.ToInt32(dgvObjects.CurrentRow.Cells[1].Value), dgvObjects.CurrentRow.Cells[5].Value.ToString());
}
}
}
not working, please point me in the right direction for this. thanks. -
2009년 9월 19일 토요일 오전 3:52Hi John, I solved the problem already, Thank you
-
2012년 3월 29일 목요일 오전 11:15
Hi
Royal Oak
I have same problem,could you please tell me how did u solve the problem?
-
2012년 4월 26일 목요일 오후 6:18
Hi royal
im new in vb.net,n i have requirement when a user enter some value in a cell at runtime it should fetch the record from the database n insert into the current row.
if u know the solution ,please help me...
Thanks
-
2012년 4월 26일 목요일 오후 6:22
I guess, this simple code above, work for that:
DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add("test1"); dt.Columns.Add("test2"); dr = dt.NewRow(); dr["test1"] = "some value"; dr["teste2"] = "some value"; dt.Rows.Add(dr);
-
2012년 5월 1일 화요일 오후 3:13What is with the fascination of resurrecting old threads attempting to provide an answer to a question that was asked and answered years ago? I really do not understand people. I see this all the time. If one has a question don't resurrect an old thread, ask a new question.
John Grove, Senior Software Engineer http://www.digitizedschematic.com/

