Maddy
Maddy

Reputation: 263

Populate data grid dynamically

while (rawCode.IndexOf("<a href") != -1)
{
                string[] parts = link.Split(new string[] { "./" }, StringSplitOptions.None);  
                string column = string.Format("Column{0}",1);
                dataGridView1.Columns.Add("Link","Links");
                for (int i = 0; i < parts.Length; i++)
                {
                    if (parts[i] != "")
                    {

                        dataGridView1.Rows.Add((new Object[] { i, parts[i]}));

                        dataGridView1.Columns.Add("Link", "Name");
                        dataGridView1.Rows.Add((new Object[] { i, parts[i]}));
                        //Here i need to insert data into already existing row.
                    }
                 }
}

I'm able to create a row dynamically using the above code. But i'm not getting how to insert data into the rows already created. Below image shows the output of above code. Data grid created using above code

I want to add the data next to cells after link. when i tried to use the same code, new rows are getting created but i want to insert data into rows already created.

Upvotes: 2

Views: 5297

Answers (1)

Kestami
Kestami

Reputation: 2073

you can update existing rows like so:

 dataTable.Rows[i]["columnNameHere"] = valueYouWantToUse;

alternatively you could input the information in the row BEFORE you add it to the datasource.

DataRow row = dataTable.NewRow();
row["columnName"] = valueYouWantToUse;
//any other information can be put here too, for each column in the row.
dataTable.Rows.Add(row);

 if (parts[i] != "")
                {

                    dataGridView1.Rows.Add((new Object[] { i, parts[i]}));

                    dataGridView1.Columns.Add("Link", "Name");
                    DataRow row = dataTable.NewRow();
                    row["Link"] = i.ToString();
                    row["Name"] = parts[i].ToString();

                    //here add all your column values for that row in the same manner, then,
                    dataTable.Rows.Add(row);
                }

Upvotes: 2

Related Questions