Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to show 16 empty rows in a gridview of asp.net. I have a button and textbox outside the grid, on a button click, I have to insert the data from the textbox into the grid but the problem is that the data inserted after the 16 rows. How the data will be inserted in empty rows first.? I bound the datasource with null but the grid is not visible so i create the rows on pageload event.
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               DataTable dtSource = new DataTable();
                    dtSource.Columns.Add("DateTime");
                    dtSource.Columns.Add("Detail");
                    dtSource.Columns.Add("Status");
                    dtSource.Columns.Add("Cancel");
                   for (int i = 0; i <= 15; i++)
                     {
                       dtSource.Rows.Add("", " ", " ", " ");
                     }
                    ViewState["dtSource"] = dtSource;
                    gridItem.DataSource = dtSource;
                    gridItem.DataBind();

            }
        }

and here is the code for the button

C#
protected void btnAdd(object sender, EventArgs e)
        {
            DataTable dtSource = ViewState["dtSource"] as DataTable;          
            DataRow dr = dtSource.NewRow();
            dtSource.Rows.Add(DateTime.Now, itemDetail, "", "");
            gridItem.DataSource = dtSource;
            gridItem.DataBind();
            ViewState["dtSource"] = dtSource;
        }
Posted
Comments
JagadishBB 28-Sep-15 13:06pm    
Before adding to the row to table , clone a new table without blank rows and add it , balance again you can add it again
F.mas27 28-Sep-15 13:12pm    
I am new to this. Wil you please elaborate it with example. Thankyou
Rojalin Sahoo 29-Sep-15 2:12am    
Don't known about your requirement.you can try dynamic grid-view. visit this may help you https://rojalinsahoo.wordpress.com/2015/08/01/dynamic-gridview/

As per your code you written

DataRow dr = dtSource.NewRow(); // this line add new rows to grid-view
dtSource.Rows.Add(DateTime.Now, itemDetail, "", "");


May this solution help you:

C#
protected void btn1_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(text1.Text))
            {
            string itemDetail = text1.Text;
            DataTable dtSource = ViewState["dtSource"] as DataTable;
            if(dtSource.Rows.Count<16) //change the values in blank field
            {
              foreach(DataRow dr in dtSource.Rows)
              {
                if (dr["DateTime"].ToString() == "" && dr["Detail"].ToString() == " ")
                  {
                    dr["DateTime"] = DateTime.Now.ToString();
                    dr["Detail"] = itemDetail;
                    break;
                    }
                }
            }
            else{ // add new row if no rows are blank.
              DataRow dr = dtSource.NewRow();
              dtSource.Rows.Add(DateTime.Now, itemDetail, "", "");
            }
            grid1.DataSource = dtSource;
            grid1.DataBind();
            ViewState["dtSource"] = dtSource;
        }
        }
 
Share this answer
 
v2
Comments
F.mas27 29-Sep-15 4:10am    
Thankyou for your help, on pageload it show 16 empty records but after 16 records it still goes into if statement bc the grid.rows.count is still 16 and therefore the data is not shown after 16 records.
Rojalin Sahoo 29-Sep-15 5:22am    
I checked this with my system. it's adding new row after 16 because condition is like if(count<16) then update else add new. so do check your condition and curly braces properly.
F.mas27 29-Sep-15 5:54am    
Thankyou so much i just change my code little bit and it works, thank for your guidance..
Rojalin Sahoo 29-Sep-15 5:56am    
I am glad that I helped you.
F.mas27 29-Sep-15 6:00am    
I am very thankful of you. but my default value of count is equal to the if condition therefore it always goes in to the loop. so i change the condition
if(gridItem.Rows[15].Cells[1].Text == " ") then update else add so its works :)
Try this
Change your variables accordingly

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
DataTable dtSource = new DataTable();
dtSource.Columns.Add("DateTime");
dtSource.Columns.Add("Detail");
dtSource.Columns.Add("Status");
dtSource.Columns.Add("Cancel");

ViewState["dtSource"] = dtSource;
gvCustomers.DataSource = dtSource;
gvCustomers.DataBind();
}
catch (Exception ex)
{ }
}
}

protected void btnClick_Click(object sender, EventArgs e)
{
try
{
DataTable dtSource = ViewState["dtSource"] as DataTable;
dtSource.Rows.Add(DateTime.Now, txtFill.Text, " ", " ");
gvCustomers.DataSource = dtSource;
gvCustomers.DataBind();
}
catch (Exception ex)
{ }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900