// Adding New row in data-table var row = new object[] { "0", "00000", "n/a", "n/a", DateTime.Now, "Create New Patient", 0, 0, 0, "n/a", "n/a", "n,a", "n/a" }; var newRow = patients.NewRow(); // patients is the object of the data-table newRow.ItemArray = row; patients.Rows.InsertAt(newRow, 0); //Binding the Combobox cmbPatients.DataSource = patients; cmbPatients.DataBind();
In above code I am first creating the array of object type, I am creating the array because this will also helpful in case when we’re using the Stored Procedure, so in this case we don’t know about the exact type of the values which being returned in data-table, so object array would be helpful to handle this case.
Next I created the new row of the data-table type, after this I assigned the object array to the row and finally inserted this newly created row at the 0 Index, means first row.
Another way to add Datarow in DataTable
We can also insert data for selected columns, except insert all data at once, I am going to show how we can do this.
// Adding New row in data-table var newRow = patients.NewRow(); // patients is the object of the data-table newRow["ColumnName"] = "Your Values"; newRow["ColumnName1"] = "Your Values"; patients.Rows.InsertAt(newRow, 0); //Binding the Combobox cmbPatients.DataSource = patients; cmbPatients.DataBind();
So you noticed that in this case I am only assigning the values for selected columns not for all. You can use either of ways according to your need. Hop you will enjoy this.
If you have any query then please contact us, we’ll be happy to help you