0

I have aspx code of grid view as follows..

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                    AutoGenerateEditButton="True" BackColor="White" BorderColor="#CC9966" 
                    BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                    DataKeyNames="Machine no." EnableModelValidation="True" 
                    onrowcancelingedit="GridView1_RowCancelingEdit" 
                    onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:BoundField DataField="Machine no." HeaderText="Machine no." 
                            ReadOnly="True" SortExpression="Machine no." />
                        <asp:BoundField DataField="Machine Name" HeaderText="Machine Name" 
                            SortExpression="Machine Name" />
                        <asp:TemplateField HeaderText="Is active?" SortExpression="Is active?">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("[Is active?]") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" 
                                    SelectedValue='<%# Bind("[Is active?]") %>'>
                                    <asp:ListItem Value="0">Inactive</asp:ListItem>
                                    <asp:ListItem Value="1">Active</asp:ListItem>
                                </asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                    <RowStyle BackColor="White" ForeColor="#330099" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                </asp:GridView>

I have defined the logic in code behind as follows..

CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
     if (!Page.IsPostBack)
     {
          bindgrid();
     }

}
void bindgrid()
{
     string q = "SELECT machineno AS 'Machine no.',machinename AS 'Machine Name',active AS 'Is active?' FROM machinedetails";
     DataTable dt = dtu.table(q, out error);
     GridView1.DataSource = dt;
     GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
     GridView1.EditIndex = e.NewEditIndex;
     bindgrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
     GridView1.EditIndex = -1;
     bindgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     string mno = e.Keys[0].ToString();
     string mname = e.NewValues[0].ToString();
     string active = e.NewValues[1].ToString();
     string q = "update machinedetails set machinename = '"+mname+"',active='"+active+"' where machineno =" + mno;
     Response.Write(q);
     e.Cancel=true;
     GridView1.EditIndex = -1;
     bindgrid();
}

But it throws the error:-

   Index was out of range. Must be non-negative and less than the size of the collection.
   Parameter name: index

can any one help me with this??

Steve --> as per your question i changed my logic as below

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     string mno = GridView1.DataKeys[e.RowIndex][0].ToString();
     GridView gv = (GridView)sender;
     for (int i = 0; i < gv.Columns.Count; i++)
     {
          DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
          gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
     }
     string mname = e.NewValues[0].ToString();
     string active = e.NewValues[1].ToString();
     string q = "update machinedetails set machinename = '" + mname + "',active='" + active + "' where machineno =" + mno;
     Response.Write(q);
     e.Cancel=true;
     GridView1.EditIndex = -1;
     bindgrid();
}

Now I dont get any error but mname is coming same as datakey my response.write code produse the following output -->update machinedetails set machinename = '2',active='0' where machineno =2

machinename = '2' should be the value that i have entered in the textbox. Dropdownlist value is comming correct.

Ratna
  • 2,289
  • 3
  • 26
  • 50
  • string mno = e.Keys[0].ToString(); throws error as well as tring mname = e.NewValues[0].ToString(); basically all values are coming null. – Ratna Mar 15 '13 at 12:06
  • Use intellisense, put a breakpoint on those lines, and look at the properties. – Mike C. Mar 15 '13 at 12:10
  • 1
    Take a look at this post: http://stackoverflow.com/questions/2429864/gridviews-newvalues-and-oldvalues-empty-in-the-onrowupdating-event – Steve's a D Mar 15 '13 at 12:15
  • changing string mno = e.Keys[0].ToString(); to string mno = GridView1.DataKeys[e.RowIndex][0].ToString(); has solved one problem but now i am getting error on the next line i.e string mname = e.NewValues[0].ToString(); – Ratna Mar 15 '13 at 12:17

2 Answers2

0

I have solved this problem by converting all fields to tempelate fields. But still don't know why this happens.

Ratna
  • 2,289
  • 3
  • 26
  • 50
0

I think I can explain why you're getting the value of MachineNo in mname.

You're saying:

 for (int i = 0; i < gv.Columns.Count; i++)
 {
      DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
      gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
 }

Then you say

 string mname = e.NewValues[0].ToString();

But what is going into Column[0]? It's Machine No:

 <asp:BoundField DataField="Machine no." HeaderText="Machine no."                             ReadOnly="True" SortExpression="Machine no." />
Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • since column 0 is read only therefore e.NewValues[0] will hold the cell[1] value. – Ratna Mar 16 '13 at 04:40
  • But you passed True to the `ExtractValuesFromCell` method, which indicates that read-only fields should be included in the collection. See here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfield.extractvaluesfromcell.aspx – Ann L. Mar 16 '13 at 17:26