Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the value of the gridview cell in edit mode and then update it.I have given Id as my data key value.But the gridview cell text appears to be null.Can anyone suggest how to solve this.This is what i have done so far.i am getting a null value in my value string.This is what i have done so far for my master gridview.

Master gridview code:
    protected void gvweeklysum_RowCommand(object sender, GridViewCommandEventArgs e)
    {

         if (e.CommandName.ToString() == "ColumnClick")
        {
            foreach (GridViewRow r in gvweeklysum.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    for (int columnIndex = 0; columnIndex < r.Cells.Count; columnIndex++)
                    {
                        r.Cells[columnIndex].Attributes["style"] += "background-color:White;";
                    }
                }
            }
            int selectedRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
            int selectedColumnIndex = Convert.ToInt32(Request.Form["__EVENTARGUMENT"].ToString());
            gvweeklysum.Rows[selectedRowIndex].Cells[selectedColumnIndex].Attributes["style"] += "background-color:Red;";



             // get the header of gridview to check which col is clicked.
            string headertext = gvweeklysum.Columns[selectedColumnIndex].HeaderText;


            if (headertext == "Weekending")  
            {
                // null condition and a else condition for a blank col in gridview 
                string weekending = gvweeklysum.Rows[selectedRowIndex].Cells[selectedColumnIndex].Text;
                DateTime weekendingdate = Convert.ToDateTime(weekending);
                DateTime getweekbegin = weekendingdate.AddDays(-6);


                var sqlFormattedDate = weekendingdate.Date.ToString("yyyy-MM-dd HH:mm:ss");
                var weekbegin = getweekbegin.Date.ToString("yyyy-MM-dd HH:mm:ss");



                DataTable dtfrweekending = new DataTable();
                connfordata.Open();
                SqlCommand cmdfrdate = new SqlCommand("select s001_Weekending as Weekend,s001_hrsday1 as Monday,s001_hrsday2 as Tuesday,s001_hrsday3 as Wednesday,s001_hrsday4 as Thursday,s001_hrsday5 as Friday,s001_hrsday6 as Saturday,s001_hrsday7 as Sunday from dbo.s001_Timesheets where s001_weekending between  '" + weekbegin + "' and '" + sqlFormattedDate + "' ", connfordata);
                SqlDataAdapter dafrdate = new SqlDataAdapter(cmdfrdate);
                dafrdate.Fill(dtfrweekending);
                connfordata.Close();

                GridView1.DataSource = dtfrweekending;
                GridView1.DataBind();
                

            }


            else if (headertext == "Monday")
            {
                // null condition and a else condition for a blank col in gridview 
                string weekending = gvweeklysum.Rows[selectedRowIndex].Cells[1].Text;
                DateTime getweekending = Convert.ToDateTime(weekending);
                DateTime dtmonday = getweekending.AddDays(-6);
                DateTime castdttime = dtmonday.AddHours(12);
                var monday = castdttime.Date.ToString("yyyy-MM-dd 12:mm:ss");

                DataTable dtfrmonday = new DataTable();

                connfordata.Open();
                SqlCommand cmdfrmonday = new SqlCommand("select s001_Weekending as Weekend,s001_hrsday1 as Monday,s001_TimesheetsID as Id from dbo.s001_Timesheets  where s001_CreatedDate = cast('" + monday + "' as datetime) ", connfordata);
                SqlDataAdapter dafrmonday = new SqlDataAdapter(cmdfrmonday);
                dafrmonday.Fill(dtfrmonday);
                connfordata.Close();

                GridView1.DataSource = dtfrmonday;
                
                GridView1.DataBind();
            }

            else if (headertext == "Tuesday")
            {
                // null condition and a else condition for a blank col in gridview 
                string weekending = gvweeklysum.Rows[selectedRowIndex].Cells[1].Text;
                DateTime getweekending = Convert.ToDateTime(weekending);
                DateTime dtmonday = getweekending.AddDays(-5);
                DateTime castdttime = dtmonday.AddHours(12);
                var monday = castdttime.Date.ToString("yyyy-MM-dd 12:mm:ss");

                DataTable dtfrmonday = new DataTable();

                connfordata.Open();
                SqlCommand cmdfrmonday = new SqlCommand("select s001_Weekending as Weekend,s001_hrsday2 as Tuesday,s001_TimesheetsID as Id from dbo.s001_Timesheets  where s001_CreatedDate = cast('" + monday + "' as datetime) ", connfordata);
                SqlDataAdapter dafrmonday = new SqlDataAdapter(cmdfrmonday);
                dafrmonday.Fill(dtfrmonday);
                connfordata.Close();

                GridView1.DataSource = dtfrmonday;
                GridView1.DataBind();




            }
    }


Now i have to perform edit on gridview1.I cant use boundfield here because everytime col names are different.Please suggest possible solution.
Posted
Updated 13-Jul-13 9:43am
v3
Comments
Dholakiya Ankit 13-Jul-13 8:31am    
give me full code what you have used template field or bound fields
Amit Karyekar 13-Jul-13 12:22pm    
@ankit:i have neither used template nor bound because i am binding the gridview dynamically.
Dholakiya Ankit 13-Jul-13 13:16pm    
then how will you get value from gridview can post some code for binding so i can understand your flow
Amit Karyekar 13-Jul-13 15:44pm    
@aniket: I have changed my question.Please take a look over it now.
[no name] 15-Jul-13 23:08pm    
Try my code in gridview rowdatabound event

1 solution

VB
If e.Row.RowType = DataControlRowType.DataRow AndAlso dgrd_WWWH_How.EditIndex = e.Row.RowIndex Then
Dim ddlresp As DropDownList = CType(e.Row.FindControl("ddlResp"), DropDownList)
Dim txtresp = CType(e.Row.FindControl("txtResp"), Label)
binddropdownlist()
ddlresp.Items.FindByText(txtresp.Text).Selected = True
End If


Try this code in Gridview RowDatabound Event
 
Share this answer
 
v2

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