I need to build a function to dynamically add rows to a gridview from form objects such as dropdowns and text fields.
I need to do this without touching a database.
And I need to give the user the ability to add multiple rows one at a time.
This is what I have come up with so far.
I get a null reference on the if
var dt = new DataTable();
dt = GridView1.DataSource as DataTable;
if (dt.Columns.Count == 0)
{
dt.Columns.Add("Field");
dt.Columns.Add("Value");
}
DataRow dr = dt.NewRow();
dr["Field"] = DropDownList1.SelectedValue;
dr["Value"] = TextBox2.Text.Trim();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();