C# 在gridview中编辑和更新

C# 在gridview中编辑和更新,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试使用gridview本身编辑和更新我的db。我几乎做了所有的事情,但我唯一的问题是我有一个下拉列表。我对编辑标签很满意,并在编辑模式下将其更改为下拉列表。现在我无法将下拉值单独更新到我的db中。我得到一个名为system.web的值。我无法获得期望值由于文字控制,我面临困难。我是一个新手,所以请帮助我。提前感谢各位朋友。 我的设计: <asp:GridView runat ="server" GridLines = "Both" DataKeyNames="book_id"

我正在尝试使用gridview本身编辑和更新我的db。我几乎做了所有的事情,但我唯一的问题是我有一个下拉列表。我对编辑标签很满意,并在编辑模式下将其更改为下拉列表。现在我无法将下拉值单独更新到我的db中。我得到一个名为system.web的值。我无法获得期望值由于文字控制,我面临困难。我是一个新手,所以请帮助我。提前感谢各位朋友。 我的设计:

<asp:GridView runat ="server"  GridLines = "Both" DataKeyNames="book_id"  
 AutoGenerateColumns ="false" CellPadding ="5" CellSpacing ="5" allowpaging="True" allowsorting="True"
 ID="gv_table1" EmptyDataText ="No data exists" OnRowEditing="gv_RowEditing" 
        PageIndex="0" PageSize="10" ToolTip="true"
OnRowCancelingEdit="gv_RowCancelingEdit" OnRowUpdating="gv_RowUpdating" 
        OnRowDeleting="gv_RowDeleting" onpageindexchanging="gv_table1_PageIndexChanging"  
>
<Columns>





<asp:BoundField DataField="book_name"   HeaderText="BookName">
<ControlStyle Width ="60" />
</asp:BoundField>
<asp:BoundField DataField="author_name" HeaderText="Author Name">
<ControlStyle Width ="60" />
</asp:BoundField>
<asp:BoundField DataField="publisher" HeaderText="Publisher">
<ControlStyle Width ="60" />
</asp:BoundField>
<asp:BoundField DataField="year_edition" HeaderText="Year/Edition">
<ControlStyle Width ="60" />
</asp:BoundField>
<asp:BoundField DataField="total_no" HeaderText="Total No">
<ControlStyle Width ="30" />
</asp:BoundField>
<asp:BoundField DataField="available" HeaderText="Available">
<ControlStyle Width ="30" />
</asp:BoundField>
<asp:BoundField DataField="tags" HeaderText="Tags">
<ControlStyle Width ="60" />
</asp:BoundField>
<asp:BoundField DataField="fare" HeaderText="Fare">
<ControlStyle Width ="30" />
</asp:BoundField>
<asp:TemplateField HeaderText="state">
                            <ItemTemplate>
                                <asp:Label ID="drpstatus1" AppendDataBoundItems="True"   Text='<%# Bind("state") %>'   Width ="60" runat="server">

                                </asp:Label >

                            </ItemTemplate>
                            <EditItemTemplate >
                           <asp:DropDownList ID="drpstatus" runat="server"    >
                                <asp:ListItem Text="available" Value="0"></asp:ListItem>
                                <asp:ListItem Text="binding" Value="1"></asp:ListItem>
                                <asp:ListItem Text="lost" Value ="2"></asp:ListItem>
                                <asp:ListItem Text ="notavailable" Value ="3"></asp:ListItem>
                            </asp:DropDownList>


                        </EditItemTemplate>

                            </asp:TemplateField>



 <asp:TemplateField HeaderText ="Options">

                                <itemtemplate >

                                        <asp:linkbutton id="btnEdit" runat="server" commandname="Edit" text="Edit" />

                                        <asp:linkbutton id="btnDelete" runat="server" commandname="Delete" text="Delete" />
                                </itemtemplate>
                                <edititemtemplate>
                                        <asp:linkbutton id="btnUpdate" runat="server" commandname="Update" text="Update" />
                                        <asp:linkbutton id="btnCancel" runat="server" commandname="Cancel" text="Cancel" />
                                </edititemtemplate>
                        </asp:templatefield>





</Columns>
</asp:GridView>

我的代码隐藏:

 public void setgrid()
    {

            gv_table1.DataSource = con.GetData("select  book_id,book_name,author_name,publisher,year_edition,total_no,state ,available,tags,fare from book_info where status!='deleted'");
            gv_table1.DataBind();
              }
     protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {


        gv_table1.EditIndex = e.NewEditIndex;

        this.setgrid();
        if (!IsPostBack)
        {
            Response.Write("not post back"); 
        }

    }

    protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        gv_table1.EditIndex = -1;

        this.setgrid();
    }

    public void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {


        string totalno = (gv_table1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text;
        string available = (gv_table1.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text;
        int total = Convert.ToInt32(totalno);
        int avail = Convert.ToInt32(available);

        if (total < avail)
        {
            Page page = HttpContext.Current.Handler as Page;

            if (page != null)
            {

               string error = "available should not be greater than total no";

                ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);

            }

        }
        else
        {
            int bookid = Convert.ToInt32(gv_table1.DataKeys[e.RowIndex].Values["book_id"].ToString());
            string bookname = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[0].Controls[0])).Text;
            string fare = (gv_table1.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox).Text;


            string authorname = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
            string publisher = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
            string yearedition = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
            string tags = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[6].Controls[0])).Text;

            string spacediv3 = Convert.ToString (( LiteralControl)(gv_table1.Rows[e.RowIndex].Cells[8].Controls[0])) ;
            string s = spacediv3 ;
            string status = "active";

            con.parameters("@bookid", DBConnection.Type.eInt, bookid);
            con.parameters("@createduser", DBConnection.Type.eVarchar, "user");
            con.parameters("@status", DBConnection.Type.eVarchar, status);
            con.parameters("@bookname", DBConnection.Type.eVarchar, bookname);
            con.parameters("@authorname", DBConnection.Type.eVarchar, authorname);
            con.parameters("@publisher", DBConnection.Type.eVarchar, publisher);
            con.parameters("@yearedition", DBConnection.Type.eVarchar, yearedition);
            con.parameters("@totalno", DBConnection.Type.eInt, totalno);
            con.parameters("@available", DBConnection.Type.eInt, available);
            con.parameters("@tags", DBConnection.Type.eVarchar, tags);
            con.parameters("@fare", DBConnection.Type.eInt, fare);
            con.parameters("@state", DBConnection.Type.eVarchar, s );
            con.parameters("@createddate", DBConnection.Type.eDateTime, "");
            con.parameters("@modifieduser", DBConnection.Type.eVarchar, "");
            con.parameters("@modifieddate", DBConnection.Type.eDateTime, "");

            con.ExecProcedure("sp_books");
            Response.Redirect("book_add.aspx");
        }
    }

    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int bookid = Convert.ToInt32(gv_table1.DataKeys[e.RowIndex].Values["book_id"].ToString());
        string status = "deleted";
        con.parameters("@bookid", DBConnection.Type.eInt, bookid);
        con.parameters("@createduser", DBConnection.Type.eVarchar, "user");
        con.parameters("@status", DBConnection.Type.eVarchar, status);
        con.parameters("@bookname", DBConnection.Type.eVarchar, "");
        con.parameters("@authorname", DBConnection.Type.eVarchar, "");
        con.parameters("@publisher", DBConnection.Type.eVarchar, "");
        con.parameters("@yearedition", DBConnection.Type.eVarchar, "");
        con.parameters("@totalno", DBConnection.Type.eInt, "");
        con.parameters("@available", DBConnection.Type.eInt, "");
        con.parameters("@tags", DBConnection.Type.eVarchar, "");
        con.parameters("@fare", DBConnection.Type.eInt, "");
        con.parameters("@state", DBConnection.Type.eVarchar, "");
        con.parameters("@createddate", DBConnection.Type.eDateTime, "");
        con.parameters("@modifieduser", DBConnection.Type.eVarchar, "");
        con.parameters("@modifieddate", DBConnection.Type.eDateTime, "");

        con.ExecProcedure("sp_books");
        Response.Redirect("book_add.aspx");
    }
public void setgrid()
{
gv_table1.DataSource=con.GetData(“从图书信息中选择图书id、图书名称、作者名称、出版商、年份版本、总编号、状态、可用、标签、票价,其中状态为“已删除”);
gv_表1.DataBind();
}
受保护的无效gv_行编辑(对象发送方,GridViewEditEventArgs e)
{
gv_表1.EditIndex=e.NewEditIndex;
这是setgrid();
如果(!IsPostBack)
{
回复。写(“不发回”);
}
}
受保护的无效gv_RowCancelingEdit(对象发送方,GridViewCancelEditEventArgs e)
{
gv_表1.EditIndex=-1;
这是setgrid();
}
公共无效gv_行更新(对象发送方,GridViewUpdateEventArgs e)
{
字符串totalno=(gv_table1.Rows[e.RowIndex]。单元格[4]。控件[0]作为文本框)。文本;
可用字符串=(gv_table1.Rows[e.RowIndex]。单元格[5]。控件[0]作为文本框)。文本;
int total=将.TOINT转换为32(totalno);
int avail=转换为32(可用);
如果(总数<有效值)
{
Page Page=HttpContext.Current.Handler作为页面;
如果(第页!=null)
{
string error=“可用不应大于总数no”;
ScriptManager.RegisterStartupScript(page,page.GetType(),“err_msg”,“alert(“+error+”);”,true);
}
}
其他的
{
int bookid=Convert.ToInt32(gv_table1.DataKeys[e.RowIndex].Values[“book_id”].ToString());
字符串bookname=((文本框)(gv_table1.Rows[e.RowIndex]。单元格[0]。控件[0])。文本;
字符串费用=(gv_table1.行[e.RowIndex]。单元格[7]。控件[0]作为文本框)。文本;
字符串authorname=((文本框)(gv_table1.Rows[e.RowIndex]。单元格[1]。控件[0])。文本;
字符串发布器=((文本框)(gv_table1.Rows[e.RowIndex]。单元格[2]。控件[0])。文本;
字符串yearedition=((文本框)(gv_table1.Rows[e.RowIndex]。单元格[3]。控件[0])。文本;
字符串标记=((文本框)(gv_table1.Rows[e.RowIndex]。单元格[6]。控件[0])。文本;
string spacediv3=Convert.ToString((LiteralControl)(gv_table1.Rows[e.RowIndex].Cells[8].Controls[0]);
字符串s=spacediv3;
字符串状态=“活动”;
con.参数(“@bookid”,DBConnection.Type.eInt,bookid);
con.参数(“@createduser”,DBConnection.Type.eVarchar,“user”);
con.参数(“@status”,DBConnection.Type.eVarchar,status);
con.参数(“@bookname”,DBConnection.Type.eVarchar,bookname);
con.参数(“@authorname”,DBConnection.Type.eVarchar,authorname);
con.参数(“@publisher”,DBConnection.Type.eVarchar,publisher);
con.参数(“@yearedition”,DBConnection.Type.eVarchar,yearedition);
con.参数(“@totalno”,DBConnection.Type.eInt,totalno);
con.参数(“@available”,DBConnection.Type.eInt,available);
con.参数(“@tags”,DBConnection.Type.eVarchar,tags);
con.参数(“@fare”,DBConnection.Type.eInt,fare);
con.参数(“@state”,DBConnection.Type.eVarchar,s);
con.parameters(“@createddate”,DBConnection.Type.eDateTime,”);
con.参数(“@modifieduser”,DBConnection.Type.eVarchar,”);
con.parameters(“@modifieddate”,DBConnection.Type.eDateTime,”);
合同执行程序(“sp_书”);
重定向(“book_add.aspx”);
}
}
受保护的无效gv_行删除(对象发送方,GridViewDeleteEventArgs e)
{
int bookid=Convert.ToInt32(gv_table1.DataKeys[e.RowIndex].Values[“book_id”].ToString());
string status=“已删除”;
con.参数(“@bookid”,DBConnection.Type.eInt,bookid);
con.参数(“@createduser”,DBConnection.Type.eVarchar,“user”);
con.参数(“@status”,DBConnection.Type.eVarchar,status);
con.参数(“@bookname”,DBConnection.Type.eVarchar,”);
con.参数(“@authorname”,DBConnection.Type.eVarchar,”);
con.参数(“@publisher”,DBConnection.Type.eVarchar,”);
con.参数(“@yearedition”,DBConnection.Type.eVarchar,”);
con.参数(“@totalno”,DBConnection.Type.eInt,”);
con.参数(“@available”,DBConnection.Type.eInt,”);
con.参数(“@tags”,DBConnection.Type.eVarchar,”);
con.参数(“@fare”,DBConnection.Type.eInt,”);
con.参数(“@state”,DBConnection.Type.eVarchar,”);
con.parameters(“@createddate”,DBConnection.Type.eDateTime,”);
con.参数(“@modifieduser”,DBConnection.Type.eVarchar,”);
string spacediv3 = Convert.ToString (( LiteralControl)(gv_table1.Rows[e.RowIndex].Cells[8].Controls[0])) ;
string spacediv3 = ((LiteralControl)(gv_table1.Rows[e.RowIndex].Cells[8].Controls[0])).Text;
public void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridView gv = (GridView)sender;
    GridViewRow row = gv.Rows[e.RowIndex];
    DropDownList ddlStatus = (DropDownList)row.FindControl("drpstatus");
    String selectedStatus = ddlStatus.SelectedValue;