locked
Removing initial blank row in data table asp.net RRS feed

  • Question

  • User1997788493 posted

    Hi,

    My code is below, when i inserting row in to data table and binding in grid view, a blank row is inserting when inserting first row.how to remove that ?

    please anybody help.

    private void addfirstrecord()
            {
                // check view state is not null  
                if (ViewState["Tbl_Contract_with_Paper"] != null)
                {
                    //get datatable from view state   
                    DataTable dtCurrentTable = (DataTable)ViewState["Tbl_Contract_with_Paper"];
                    DataRow drCurrentRow = null;
                    if (dtCurrentTable.Rows.Count > 0)
                    {
                        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                        {
                            decimal minicopies = Convert.ToDecimal((GridView2.FooterRow.FindControl("TextBox2") as TextBox).Text.Trim());
                            decimal Unit_Price = Convert.ToDecimal((GridView2.FooterRow.FindControl("TextBox4") as TextBox).Text.Trim());
                            decimal amount = minicopies * Unit_Price;
                            decimal total = minicopies * 2;//Please check this value, i dnt know this.
                            //add each row into data table  
                            drCurrentRow = dtCurrentTable.NewRow();
                            drCurrentRow["Min_Copies_Paper_Transfer"] = minicopies;
                            drCurrentRow["Unit_Price"] = Unit_Price;
                            drCurrentRow["U_Type"] = (GridView2.FooterRow.FindControl("DropDownList1") as DropDownList).Text.Trim();
                            drCurrentRow["Amt_Per_Mnth"] = amount;
                            drCurrentRow["Total"] = total;
                        }
                        //Remove initial blank row  
                        if (dtCurrentTable.Rows[0][0].ToString() == "")
                        {
                            dtCurrentTable.Rows[0].Delete();
                            dtCurrentTable.AcceptChanges();
                        }
    
                        //add created Rows into dataTable  
                        dtCurrentTable.Rows.Add(drCurrentRow);
                        //Save Data table into view state after creating each row  
                        ViewState["Tbl_Contract_with_Paper"] = dtCurrentTable;
                        //Bind Gridview with latest Row  
                        GridView2.DataSource = dtCurrentTable;
                        GridView2.DataBind();
                    }
                }
            }

    Saturday, July 28, 2018 10:42 AM

All replies

  • User475983607 posted

    I'm having a hard time making any sense of the logic or the intention of the logic.  Can you explain what the code is designed to do? 

    There is a method named addfirstrecord but the code does not add the first record.  The code actually skips the first record and only runs of there is a DataTable in ViewState.  It seems to me that the DataTable has been created before addfirstrecord() runs.

    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                        {

    My best guess is the DataTable is initialized elsewhere in the code.  The initialization  code creates an empty initial record.  Since the code show above always skips the first record, the first record is always empty. 

    I strongly recommend using the Visual Studio Debugger to check that the logic is working as expected.

    Lastly, it looks like the code is redundant.  The default ASP.NET Web Forms state management will remember the GridView state.  Why are you manually overriding the default behavior?

    Saturday, July 28, 2018 11:03 AM
  • User1997788493 posted

    Hi,

    Thank you for your reply, this is y data table default code 

            private void AddDefaultFirstRecord()
            {
                //creating dataTable   
                DataTable dt = new DataTable();
                DataRow dr;
                dt.TableName = "Tbl_Contract_with_Paper";
                DataColumn Tbl_ID = new DataColumn();
                DataColumn myid = dt.Columns.Add("tbl_id", typeof(Int32));
                myid.AutoIncrement = true;
                myid.AutoIncrementSeed = 1;
                myid.AutoIncrementStep = 1;
                dt.Columns.Add(new DataColumn("Min_Copies_Paper_Transfer", typeof(decimal)));
                dt.Columns.Add(new DataColumn("Unit_Price", typeof(decimal)));
                dt.Columns.Add(new DataColumn("U_Type", typeof(string)));
                dt.Columns.Add(new DataColumn("Amt_Per_Mnth", typeof(decimal)));
                dt.Columns.Add(new DataColumn("Total", typeof(decimal)));
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    //saving databale into ViewState
                    ViewState["Tbl_Contract_with_Paper"] = dt;
                    //Bind gridview
                    GridView2.DataSource = dt;
       
                    GridView2.DataBind();
                }
                else
                {
                    ViewState["Tbl_Contract_with_Paper"] = dt;
                    dt.Rows.Add(dt.NewRow());
                    GridView2.DataSource = dt;
        
                    GridView2.DataBind();
                    GridView2.Rows[0].Cells.Clear();
                    GridView2.Rows[0].Cells.Add(new TableCell());
                    GridView2.Rows[0].Cells[0].ColumnSpan = dt.Columns.Count;
                    GridView2.Rows[0].Cells[0].Text = "No Data Found...!";
                    GridView2.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
                }
    
            }

    Please check 

    Saturday, July 28, 2018 11:09 AM
  • User475983607 posted

    The AddDefaultFirstRecord method clearly creates an empty initial row and adds the DataTable, with the empty row, to ViewState.   Again, the first posted code skips the first record found in ViewState.

    Frankly, the code does not make logical sense and seems over engineered.   The GridView has an empty data template so there is no reason to craft a custom empty data template.  ViewState handles the GridView state so there is not reason to store a DataTable in ViewState.

    I recommend that you go through the Getting Started with Web Forms tutorials to get a basic understanding of Web Forms.  I also recommend that you read the GridView API documentation because you are reinventing the wheel.  Lastly, learn how to use the Visual Studio debugger.

    Saturday, July 28, 2018 11:25 AM
  • User1997788493 posted

    This is not good way to answer..first you learn how to talk and learn..if i did mistake you have to say like this is wrong..? how can u say over engineered? 

    See, without basics i cant write that much code, im new to datatable thats why mistakes happen, so if you are a good All-star in Forums then answer it proper way, 

    and try to learn basics of speak and talk first.

    Saturday, July 28, 2018 11:33 AM
  • User475983607 posted

    If you go back to the first post I asked you to explain the code.   You ignored the request and posted another block of code again without any explanation whatsoever.  I do not know your level of experience or what you are trying to accomplish unless you actually tell us.

    DDNaidu

    how can u say over engineered? 

    All server controls including the GridView maintain ViewState by default.  This is a fundamental feature in Web Forms and a subject that is cover in the Getting Started tutorials. For some unknown reason you are reengineering ASP.NET state management.  You might have a good reason.  I have no idea because you have not answered the questions.

    DDNaidu

    See, without basics i cant write that much code, im new to datatable thats why mistakes happen, so if you are a good All-star in Forums then answer it proper way,

    What's proper?  If you do not understand the basics then how do you expect to understand the answer.  Keep in mind, I've explained the problem two times now.  For the third time, the first block of code skips the first record and the second block of code writes an empty first record.  However, the code seems unnecessary as fundamental Web Forms features already handle state.  

    Can you explain what you are trying to do?

    Saturday, July 28, 2018 12:06 PM
  • User-1003897979 posted

    Why do you have an initial blank first row in data table?  Because the first thing you do in the data table is add a blank row.

    if (dt.Rows.Count > 0)
    {
    .
    .
    blah
    .
    .
    }
    else // There are no rows in dt
    {
         ViewState["Tbl_Contract_with_Paper"] = dt;
         dt.Rows.Add(dt.NewRow());  // Add a blank row as the first row in dt
    

    If you don't want a blank first row in the table then don't add one!

    Monday, July 30, 2018 7:51 AM