Click here to Skip to main content
16,018,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added a gridview which is generated dynamically. I am fetching rows from DB and loading it into datatable. I want to add edit and delete button to this datatable and bind it to gridview so that i can edit and delete content of gridview. The gridview is generated more than one time and is called in loop as it is need to generate timesheet for weeks.

Here is source code for binding data and gridview generation:

C#
protected void BindData(string str)
    {
        string[] startdate = getdate(str);
        int firstdate = Convert.ToInt32(startdate[1]);
        int lastdate = firstdate + 7;
        string enddate = startdate[0]+"/"+Convert.ToString(lastdate)+"/"+startdate[2];
        if (conn.State == ConnectionState.Closed)
            conn.Open();
       
        SqlCommand cmd = new SqlCommand("SELECT DISTINCT CONVERT (varchar(25), Cale_WeekDate,106) AS 'Date',Cale_DayName AS 'Day',Cale_Task8 AS '08:00 AM',Cale_Task9 AS '09:00 AM',Cale_Task10 AS '10:00 AM',Cale_Task11 AS '11:00 AM',Cale_Task12 AS '12:00 PM',Cale_Task13 AS '1:00 PM' FROM Calendar WHERE Cale_WeekDate BETWEEN '"+str+"' AND '"+enddate+"'", conn);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);               
        GridView gvTimeSheet = new GridView();
        //gvTimeSheet.ID = "gvTimeSheet" + gridcount;
        gvTimeSheet.AutoGenerateColumns = false;

            dt = ds.Tables[0];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                BoundField boundfield = new BoundField();
                boundfield.DataField = dt.Columns[i].ColumnName.ToString();
                boundfield.HeaderText = dt.Columns[i].ColumnName.ToString();
                gvTimeSheet.Columns.Add(boundfield);
            }
            gvTimeSheet.DataSource = dt;
            gvTimeSheet.DataBind();
            gvTimeSheet.Width = 600;
            gvTimeSheet.HeaderStyle.CssClass = "header";
            gvTimeSheet.RowStyle.CssClass = "rowstyle";

            Panel1.Controls.Add(gvTimeSheet);
            HtmlGenericControl linebreak = new HtmlGenericControl("br");            
            Panel1.Controls.Add(linebreak);

        
    }
Posted
Updated 11-Jul-15 6:24am
v2

1 solution

Just add the RowEditing, RowDeleting Events and related controls dynamically as you are doing it for columns.

Just see the official documentation of these events and see what needs to be added on markup. Then try to generate the markup codes in code behind as you are already doing.
 
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