在编辑gridview时,我得到空数据键以及空的新值

本文关键字:数据 新值 gridview 编辑 | 更新日期: 2023-09-27 18:03:59

我有网格视图的aspx代码如下..

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>

我在后面的代码中定义了如下的逻辑…

后台代码>:
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();
}

但是会抛出错误:-

   Index was out of range. Must be non-negative and less than the size of the collection.
   Parameter name: index
有谁能帮我一下吗?

Steve ->根据你的问题,我改变了我的逻辑如下

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();
}

现在我没有得到任何错误,但mname和datakey一样我的回答。update machinedetails set machinename =' 2',active='0' where machineno =2

machinename = '2'应该是我在文本框中输入的值。

在编辑gridview时,我得到空数据键以及空的新值

我通过将所有字段转换为模板字段解决了这个问题。但我还是不知道为什么会这样

我想我可以解释为什么你在mname中得到MachineNo的值。

你说

:

 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();

但是进入Column[0]的是什么?它是Machine No:

 <asp:BoundField DataField="Machine no." HeaderText="Machine no."                             ReadOnly="True" SortExpression="Machine no." />