Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 13-Jul-13 2:49am
Edited 13-Jul-13 10:43am
v3
Comments
dholakiya ankit at 13-Jul-13 8:31am
   
give me full code what you have used template field or bound fields
Amit Karyekar at 13-Jul-13 12:22pm
   
@ankit:i have neither used template nor bound because i am binding the gridview dynamically.
dholakiya ankit at 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 at 13-Jul-13 15:44pm
   
@aniket: I have changed my question.Please take a look over it now.
senthil kumar - 2185 at 15-Jul-13 23:08pm
   
Try my code in gridview rowdatabound event

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 15 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100