兄弟姐妹们!大家快来帮帮我呀!当我在DataGrid中删除记录时,我应该怎样获得选中删除记录中的某一个栏位的值(不是Key哦)?我在DataGrid_Update 事件中用这种方法就行,可以在DataGrid_Del事件中总是报错,这是怎么回事呀? 
string sCategory=((TextBox)e.Item.FindControl("edit_Ca_Name")).Text.Trim();

解决方案 »

  1.   

    报错的信息如下:Object reference not set to an instance of an object. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error: 
    Line 303:// string sCategory=(Label1)e.Item.ItemIndex.ToString ();
    Line 304:// string sCategory=MyDataGrid.Items[e.Item.ItemIndex].Cells[1].Text ;
    Line 305: string sCategory=((TextBox)e.Item.FindControl("edit_Ca_Name")).Text.Trim();
    Line 306:
    Line 307:// string sCategory = (string)DataBinder.Eval(e.Item.DataItem, "Ca_Name");
     
      

  2.   

    Line 303为什么那样写。
    string sCategory=e.Item.ItemIndex.ToString ();
    而且Line 304~307都要声明sCategory,去掉其前面的string
      

  3.   

    我的DataGrid 是这样定义的,
    <asp:datagrid id="MyDataGrid" runat="server" Width="572px" AutoGenerateColumns="False" AllowPaging="True" AllowCustomPaging="True" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" OnDeleteCommand="MyDataGrid_Del" OnPageIndexChanged="MyDataGrid_Change" OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit" OnUpdateCommand="MyDataGrid_Update" ItemStyle-HorizontalAlign="Center" DataKeyField="ca_Code" AllowSorting="True" PageSize="7">
    <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
    <ItemStyle HorizontalAlign="Center" ForeColor="#003399" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#6699CC"></HeaderStyle>
    <FooterStyle ForeColor="#EEF3FF" BackColor="White"></FooterStyle>
    <Columns>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
    <ItemStyle Wrap="False" Width="40px"></ItemStyle>
    </asp:EditCommandColumn>
    <asp:BoundColumn DataField="ca_Code" SortExpression="ca_Code" ReadOnly="True" HeaderText="Code">
    <ItemStyle Wrap="False"></ItemStyle>
    </asp:BoundColumn>
    <asp:TemplateColumn SortExpression="Ca_Name" HeaderText="Category">
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Right"></ItemStyle>
    <ItemTemplate>
    <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Ca_Name") %>' >
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox runat="server" id="edit_Ca_Name" Text='<%# DataBinder.Eval(Container.DataItem, "Ca_Name") %>' MaxLength="100">
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:ButtonColumn Text="Del" CommandName="Delete"></asp:ButtonColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#EEF3FF" PageButtonCount="25" Mode="NumericPages"></PagerStyle>
    </asp:datagrid></td>我的Line 303,304,307,并且屏避掉了,真正起作用的是 Line 305呀!
      

  4.   

    没有判断e的状态吧。
    你加一句:
    if(e.Item.ItemType==ListItemType.EditItem)

      string sCategory=((TextBox)e.Item.FindControl("edit_Ca_Name")).Text.Trim();
      

  5.   

    加上去也不行呀,因为我是在DataGrid_Delelte时候抓这个栏位的值。怎么没有其他人来这里帮我呀,我快急死了!谢谢楼上的关注!
      

  6.   

    应该这样写才对
    string sCategory=((Label)e.Item.FindControl("Ca_Name")).Text.Trim();