GRIDVIEW里就一个列,定义如下:        DataTable dt = new DataTable();
        dt.Columns.Add("name");
        DataRow row = dt.NewRow();
        dt.Rows.Add("aaa");
        dt.Rows.Add("bbb");
        dt.Rows.Add("ccc");
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();然后我又定义了一个删除列,想通过按删除把前面这列的内容删掉
可是现在在RowDeleting事件中我取到的却是空值,请问为什么?        string name = this.GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();name为空,如何解决?我想达到的效果是点删除即删除该行

解决方案 »

  1.   

    这样试一下:
    string name=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
      

  2.   


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "javascript:alert('当前ID为:" +DataBinder.Eval(e.Row.DataItem,"CID") + "')");
            }
        }
    我做的是点击当前行获取行记录的ID,不过GRIDVIEW的DATAKEYSNAME要设成ID。。
      

  3.   

    先设置DATAKEYSNAME
    然后获取GridView1.DataKeys[e.RowIndex].Values[0]获取主键
      

  4.   

    前台的 gv中加列 <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" DeleteText="Delete">
                        <ItemStyle Width="100px" />
                    </asp:CommandField>后台:RowDeleting 事件中: string sqllist = "delete from  table where code ='" + GridList.DataKeys[e.RowIndex].Value.ToString() + "'";
            cmd.Delete(sqllist);
      

  5.   


    ////////////////////
    html 代码
          <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
                GridLines="None" 
                Width="100%" onpageindexchanging="GridView1_PageIndexChanging" 
                onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
                    <asp:BoundField HeaderText="广告id" DataField="Adid" />
                    <asp:BoundField DataField="Wid" HeaderText="用户id" />
                    <asp:BoundField DataField="Yid" HeaderText="订单id" />
                    <asp:HyperLinkField DataTextField="YTitle" HeaderText="订单标题" DataNavigateUrlFields="Yid" DataNavigateUrlFormatString="Manager/CustomerService/Orders/OrderInfo.aspx?id={0}" />
                    <asp:BoundField DataField="Addtime" HeaderText="订单时间" />
                    <asp:TemplateField HeaderText="管理">
                        <ItemTemplate>
                            <asp:LinkButton ID="lkbtnSettlement" CommandName="yes"  CommandArgument='<%# Bind("ID") %>'  runat="server" Text="通过" Visible="false">  </asp:LinkButton>
                            <asp:LinkButton ID="LinkButton1" CommandName="no"  CommandArgument='<%# Bind("ID") %>'  runat="server" Text="未通过" Visible="false">  </asp:LinkButton>
                            <asp:Label ID="lblStates" runat="server" Text='<%# Bind("ValidOrder") %>' Visible="false"></asp:Label>
                            <asp:Label ID="lblOk" runat="server" Visible="false"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>/////////////////////////////////
    cs代码    //修改事件
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //通过
            if (e.CommandName.Equals("yes"))
            {
                Update(e.CommandArgument.ToString(), "0");
            }
            //未通过
            if (e.CommandName.Equals("no"))
            {
                Update(e.CommandArgument.ToString(), "1");
            }
        }
        //修改状态
        private void Update(string id, string state)
        {
            try
            {
                CpaLog cpa = new CpaLog();
                if (cpa.UpdState(id, state))
                {
                     <script>alert('操作成功');</script>
                }
                else
                {
                <script>alert('操作失败');</script>
                }
            }
            catch
            {
                script>alert('操作失败');</script>
            }
        }