1

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();
Gene
  • 57
  • 6
  • https://stackoverflow.com/questions/10063770/how-to-add-a-new-row-to-datagridview-programmatically – Mihir Dave Sep 20 '19 at 15:10
  • Do you plan to run this code once only, or once per new row? – Robin Bennett Sep 20 '19 at 15:19
  • The user would fill in a dropdown and text box. Those values need to be added to the gridview. I am trying to use a datatable to move the info from the form objects to the gridview. The issue I am running into now is the form just replaces instead of adding a new row. – Gene Sep 20 '19 at 15:51

2 Answers2

1

I'm not sure what you think

var dt = new DataTable();
dt = GridView1.DataSource as DataTable;

is doing, but it's assigning null to dt (because GridView1.DataSource is null)

Try:

var dt = GridView1.DataSource as DataTable;
if (dt == null) 
{
    dt = new DataTable();
    GridView1.DataSource = dt;
}

In future, when you get a null reference error, add a breakpoint and check the value of each variable in the row to see which one is null.

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
  • I disagree. I think he is trying to grab the current data source from the gridview, add a new row to that variable, then rebind it. This is correct, just needs to initialize it if it comes back null – dvo Sep 20 '19 at 15:14
  • @dvo - maybe, but appears to be null, and he initialised `dt` in the previous line. I'll edit my answer to handle both. – Robin Bennett Sep 20 '19 at 15:16
  • Yes, he is still missing that. I just think setting the data source where you are suggesting is incorrect. – dvo Sep 20 '19 at 15:17
  • @dvo - good point, setting the data source should only be done once, while adding rows might be done multiple times. – Robin Bennett Sep 20 '19 at 15:21
  • Correct - I believe OP is looking for the ability to add one row at a time. I think you've solved his NULL Reference exception with this answer. – dvo Sep 20 '19 at 15:22
  • Thank.... this is helping. @dvo you are correct.. I am letting the user build a list of items from a text field and dropdown. I will admit, I am not the best when working with data tables. So I made the changes.. No Errors now. However.. The new data is replacing the old data instead of adding to it. ` dt.Rows.Add(dr); GridView1.DataSource = dt; GridView1.DataBind(); ` – Gene Sep 20 '19 at 15:43
  • Gene - you need to separate the creation of the data table from the code that adds rows. We're struggling because we've only got one function. – Robin Bennett Sep 20 '19 at 15:46
0

Try a ViewState variable instead of pulling the data source. There may be weird things with the rows...

Set view state with .DataBind(): ViewState["GV1_DT"] = dt;

Pull it with var dt = ViewState["GV1_DT"] as DataTable; Then you can incorporate your null check by doing: var dt = (ViewState["GV1_DT"] as DataTable) ?? new DataTable();

Full code:

    var dt = (ViewState["GV1_DT"] as DataTable) ?? new DataTable(); // -- PULL

    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);

    ViewState["GV1_DT"] = dt;    // -- SET
    GridView1.DataSource = dt;
    GridView1.DataBind();
dvo
  • 2,113
  • 1
  • 8
  • 19