Add new row to datagridview one by one dynamically
-
venerdì 18 settembre 2009 23: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;
Tutte le risposte
-
sabato 19 settembre 2009 00: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- Proposto come risposta paras kumarMicrosoft Employee sabato 19 settembre 2009 02:50
- Proposta come risposta annullata Royal Oak sabato 19 settembre 2009 03:51
-
sabato 19 settembre 2009 00: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- Contrassegnato come risposta Royal Oak sabato 19 settembre 2009 03:51
-
sabato 19 settembre 2009 03: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. -
sabato 19 settembre 2009 03:52Hi John, I solved the problem already, Thank you
-
giovedì 29 marzo 2012 11:15
Hi
Royal Oak
I have same problem,could you please tell me how did u solve the problem?
-
giovedì 26 aprile 2012 18: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
-
giovedì 26 aprile 2012 18: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);
-
martedì 1 maggio 2012 15: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/

