首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

gridview 怎么删除一行记录

2012-01-19 
gridview 如何删除一行记录大家注意我的datasource,我猜测这是我用的很多code不起作用的原因。在我的程序中

gridview 如何删除一行记录
大家注意我的datasource,我猜测这是我用的很多code不起作用的原因。在我的程序中,gv.DataKeys.Count=0!

C# code
DataTable dt = DB.GetAllLines(Int32.Parse(lblGameID.Text));gv.DataSource = dt;gv.DataBind();


gridview的设置:
HTML code
            <asp:GridView ID="gv" runat="server" AutoGenerateDeleteButton="True"                 BorderColor="#99FF99" BorderStyle="Solid" BorderWidth="1px"                 EmptyDataText="没有任何选项" AutoGenerateColumns="False"                 onrowdeleting="gv_RowDeleting">                <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />                <Columns>                    <asp:BoundField DataField="line_id" Visible="False" />                    <asp:BoundField DataField="line_text" HeaderText="选项"/>                    <asp:BoundField DataField="line_odds" HeaderText="赔率"/>                    <asp:BoundField DataField="line_status" HeaderText="状态" />                </Columns>                <HeaderStyle BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center"                     VerticalAlign="Middle" />                <AlternatingRowStyle BackColor="#9999FF" HorizontalAlign="Center"                     VerticalAlign="Middle" Width="695px" Wrap="False" />            </asp:GridView>


这个是我的一个尝试:
C# code
        protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)        {                       GridViewRow row = gv.Rows[e.RowIndex];            int result = DB.DeleteLine(Int32.Parse(row.Cells[0].ToString()));            gv.DeleteRow(e.RowIndex);                    }


[解决办法]
晕,你gridview不定义DataKeyNames,用DataKeyNames绑定你数据库主键,这样删除就OK拉
string id = this.GridView1.DataKeys[e.RowIndex]["id"].ToString();
string deda = "delete from tablename where id='" + id + "'";

<asp:GridView ID="GridView1" runat="server" Width="603px" Font-Size="12px" ForeColor="#333333"
AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:HyperLinkField DataTextField="company_name" DataNavigateUrlFields="id" DataNavigateUrlFormatString="UpdateJobCompany.aspx?Id={0}"
HeaderText="公司名称">
<HeaderStyle Width="350px" HorizontalAlign="Center" />
<ItemStyle Width="350px" HorizontalAlign="Center" />
</asp:HyperLinkField>
<asp:BoundField DataField="company_address" HeaderText="公司地址" SortExpression="company_address">
<HeaderStyle Width="350px" HorizontalAlign="Center" />
<ItemStyle Width="350px" HorizontalAlign="Center" />


</asp:BoundField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True">
<HeaderStyle Width="100px" HorizontalAlign="Center" />
<ItemStyle Width="100px" HorizontalAlign="Center" />
</asp:CommandField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
[解决办法]
你在<asp:GridView DataKeyNames="line_id">加那个属性,
在你的删除事件里取删除行的时候 this.gv.DataKeys[e.RowIndex].Value.ToString()

[解决办法]
girdview 的DataKeyNames="ID"

C# code
    protected void GvRule_RowDeleting(object sender, GridViewDeleteEventArgs e)    {        int id = Convert.ToInt32(GvRule.DataKeys[e.RowIndex].Value);        try        {            DbHelperSQL.ExecuteSql("delete from table where ID=" + id);            Getroleinfos();        }        catch (Exception ex)        {            throw ex;        }    }
[解决办法]
根据line_id删,不要用GridView自带的删除
在RowCommand事件里写代码
[解决办法]
<asp:GridView DataKeyNames="line_id">
[解决办法]
<asp:GridView ID="gv" runat="server" AutoGenerateDeleteButton="True" 
BorderColor="#99FF99" BorderStyle="Solid" BorderWidth="1px" 
EmptyDataText="没有任何选项" AutoGenerateColumns="False" 
onrowdeleting="gv_RowDeleting" DataKeyNames="line_id">





protected void GvRule_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GvRule.DataKeys[e.RowIndex].Value);
try
{
DbHelperSQL.ExecuteSql("delete from table where ID=" + id);
Getroleinfos();
}
catch (Exception ex)
{
throw ex;
}
}

热点排行