moe February 2016

Add new rows to gridview in asp.net

As i add new rows, i want the default values for the new rows to be zero. The new rows are added now when i click the addNewRows button but they are just blank. How can i preassign to zero value for each of the column in the row?

 private void AddNewRow()
    {
      int rowIndex = 0;

      if (ViewState["CurrentTable"] != null)
      {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count > 0)
        {
          for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
          {
            DropDownList ddlTaskCurrWeek = (DropDownList)myGridView.Rows[rowIndex].Cells[1].FindControl("ddlTaskCurrWeek");
            TextBox TextBoxDay1 = (TextBox)myGridView.Rows[rowIndex].Cells[2].FindControl("txtDay1");
            TextBox TextBoxDay2 = (TextBox)myGridView.Rows[rowIndex].Cells[3].FindControl("txtDay2");
            TextBox TextBoxDay3 = (TextBox)myGridView.Rows[rowIndex].Cells[4].FindControl("txtDay3");
            TextBox TextBoxDay4 = (TextBox)myGridView.Rows[rowIndex].Cells[5].FindControl("txtDay4");
            TextBox TextBoxDay5 = (TextBox)myGridView.Rows[rowIndex].Cells[6].FindControl("txtDay5");
            TextBox TextBoxDay6 = (TextBox)myGridView.Rows[rowIndex].Cells[7].FindControl("txtDay6");
            TextBox TextBoxDay7 = (TextBox)myGridView.Rows[rowIndex].Cells[8].FindControl("txtDay7");
            Label lbl8 = (Label)myGridView.Rows[rowIndex].Cells[9].FindControl("lblTotal");
            // TextBox TextBoxDay8 = (TextBox)myGridView.Rows[rowIndex].Cells[9].FindControl("txtDay8");


            drCurrentRow = dtCurrentTable.NewRow();
            drCurrentRow["RowNumber"] = i + 1;
            dtCurrentTable.Rows[i - 1]["Col1"] = ddlTaskCurrWeek.SelectedValue;
            dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxDay1.Text;
            dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxDay2.Text;
            dtCurrentTable.Rows[i - 1]["Col4"]        

Answers


Dave Bush February 2016

You are initializing the new row from your form fields using:

      drCurrentRow = dtCurrentTable.NewRow();
      drCurrentRow["RowNumber"] = i + 1;
      dtCurrentTable.Rows[i - 1]["Col1"] = ddlTaskCurrWeek.SelectedValue;
      dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxDay1.Text;
      dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxDay2.Text;
      dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxDay3.Text;
      dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxDay4.Text;
      dtCurrentTable.Rows[i - 1]["Col6"] = TextBoxDay5.Text;
      dtCurrentTable.Rows[i - 1]["Col7"] = TextBoxDay6.Text;
      dtCurrentTable.Rows[i - 1]["Col8"] = TextBoxDay7.Text;
      dtCurrentTable.Rows[i - 1]["Col9"] = lbl8.Text;

Instead, initialize them with what you want them to have:

      drCurrentRow = dtCurrentTable.NewRow();
      drCurrentRow["RowNumber"] = i + 1;
      dtCurrentTable.Rows[i - 1]["Col1"] = "0";
      dtCurrentTable.Rows[i - 1]["Col2"] = "0";

etc ...

I'm assuming here that the columns are looking for string values because of the way your code is setup but you can set whatever.

You also need to add the row to the dataset. Your code gives you a row that you can work with. But the dataset doesn't know about it yet (yeah, I know, you'd think it would but it doesn't)

So, you also need

dtCurrentTable.Rows.Add(drCurrentRow);

somewhere. I'd put it after the initialization of the columns

https://msdn.microsoft.com/en-us/library/5ycd1034.aspx

And as has already been mentioned, you are initializing for each row.


devlin carnate February 2016

First, I think you have a problem with your existing logic. It appears that you are iterating over all existing rows in your table, and at each iteration, you are creating a new row at the current index + 1. But you only add this row to the table at the very end. This means if you have 10 existing rows in your table, you're actually creating 10 new rows, but only the last iteration actually adds a row to the table, so the net affect is 1 new row. If this is not your intention, a better approach would be:

//outside the loop
drNewRow = dtCurrentTable.NewRow();
drNewRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
drNewRow["Col1"] = "0"; //assuming your drop down has a default value of 0.
drNewRow["Col2"] = "0";
//etc..
dtCurrentTable.Rows.Add(drNewRow);

Post Status

Asked in February 2016
Viewed 3,930 times
Voted 5
Answered 2 times

Search




Leave an answer