Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I want to add template field into the gridview on button click event. Following is my code which is working fine but only for one time so please let me know to how add multiple rows on button click.
C#
 private void SetInitialRow()
        {
            DataTable dt = new DataTable();

                if (ddlBroker.SelectedIndex > 0 && ViewState["CurrentTable"] == null) 
                {
                    objBEL.Broker_id = int.Parse(ddlBroker.SelectedValue.ToString());
                    objBEL.city_id = objBLL.GetCityId(objBEL);
                    dt = objBLL.GetRecords(objBEL);

     
                    if (dt.Rows.Count > 0)
                    {

                        DataRow dr = null;

                        gdvRates.DataSource = ViewState["CurrentTable"];
                        gdvRates.DataBind();

                        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
                        dr = dt.NewRow();
                        dr["RowNumber"] = 1;
                        dr["Subcommodity_name"] = string.Empty;
                        dr["City_name"] = string.Empty;
                        dr["Brand_Name"] = string.Empty;
                        dr["Brand_Desc"] = string.Empty;
                        dr["Min_Rate"] = DBNull.Value;
                        dr["Max_Rate"] = DBNull.Value;
                        dr["Brand_Abbr"] = string.Empty;
                        dr["Brand_Id"] = DBNull.Value;
                        dr["Rate_id"] = DBNull.Value;

                        dt.Rows.Add(dr);

                        ViewState["CurrentTable"] = dt;

                        gdvRates.DataSource = ViewState["CurrentTable"];
                        gdvRates.DataBind();


                    }
                }

                else
                {
                    DataRow dr = null;
dt = (DataTable)ViewState["CurrentTable"];

                 //   gdvRates.DataSource = ViewState["CurrentTable"];
                  //  gdvRates.DataBind();

                    dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
                    dt.Columns.Add(new DataColumn("Subcommodity_name", typeof(string)));
                    dt.Columns.Add(new DataColumn("City_name", typeof(string)));
                    dt.Columns.Add(new DataColumn("Brand_Name", typeof(string)));
                    dt.Columns.Add(new DataColumn("Brand_Abbr", typeof(string)));
                    dt.Columns.Add(new DataColumn("Brand_Desc", typeof(string)));
                    dt.Columns.Add(new DataColumn("Min_Rate", typeof(string)));
                    dt.Columns.Add(new DataColumn("Max_Rate", typeof(string)));
                    dt.Columns.Add(new DataColumn("Brand_Id", typeof(string)));
                    dt.Columns.Add(new DataColumn("Rate_id", typeof(string)));

                    dr = dt.NewRow();
                    dr["RowNumber"] = 1;
                    dr["Subcommodity_name"] = string.Empty;
                    dr["City_name"] = string.Empty;
                    dr["Brand_Name"] = string.Empty;
                    dr["Brand_Desc"] = string.Empty;
                    dr["Min_Rate"] = DBNull.Value;
                    dr["Max_Rate"] = DBNull.Value;
                    dr["Brand_Abbr"] = string.Empty;
                    dr["Brand_Id"] = DBNull.Value;
                    dr["Rate_id"] = DBNull.Value;

                    dt.Rows.Add(dr);

                    ViewState["CurrentTable"] = dt;

                    gdvRates.DataSource = ViewState["CurrentTable"];
                    gdvRates.DataBind();


                }
                       
        }


Thanks in advance.
Posted
Updated 17-Jun-13 0:14am
v6

1 solution

On every post back your dynamic data is lost. So you need to recreate that data again or you can put that data in Session or ViewState and retrieve it from there inside SetInitialRow method. That's why its giving you impression that its working only once.
 
Share this answer
 
v2
Comments
bhagyap 17-Jun-13 3:24am    
but i had added ViewState["CurrentTable"] = dt; to the datatable but still it didnt give me proper results. Can you tel me,wat am i missing??
Mahesh Bailwal 17-Jun-13 3:45am    
You also need to retrieve datatable from ViewState. Please refer below mention code


If(ViewState["CurrentTable"] == null)
{
objBEL.Broker_id = int.Parse(ddlBroker.SelectedValue.ToString());
objBEL.city_id = objBLL.GetCityId(objBEL);
dt = objBLL.GetRecords(objBEL);
}
else
{
dt = ViewState["CurrentTable"];
}

bhagyap 17-Jun-13 3:59am    
no,not working..
Mahesh Bailwal 17-Jun-13 4:04am    
Can you share your updated code.
bhagyap 17-Jun-13 4:52am    
ya sure..

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