0

我将 Dropdownlist 放在我的 GridView1 中,下拉列表的值是 0 和 1。我想要的是:根据从 dropDownlist 中选择的值来更新我的 sql 数据库中的值列 [Status] ???

protected void SQL_Update(object sender, GridViewUpdateEventArgs e){

SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Korisnik;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE RegistracijaKorisnik SET Status = " + ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2")).SelectedValue; ;
cmd.Connection = conn;

conn.Open();
cmd.ExecuteNonQuery();

conn.Close();}
4

2 回答 2

0

如果我是你,根据你提供的信息,我认为解决方案是

<asp:TemplateField />

带有 DropDownList 的列

<EditTemplate>

本专栏的。然后,您需要 GridView 中的某种 Button

CommandName="Edit"

这将触发 GridView 的编辑事件,您可以使用该事件来识别正在编辑和设置的行的索引

GridView1.EditIndex = e.NewEditIndex

现在在该行中,您将拥有值 0 和 1 的 DropDownList。您还需要在 EditTemplate 中有一个 Button,它将保存更改

CommandName="Update"

然后在触发 RowUpdating 时处理网格的 RowUpdating 事件以从 DropdownList 中查找新值,使用此新数据更新数据库中的相关行,然后将 GridView 的 EditIndex 属性设置回“-1”以停止编辑该行。然后,您希望重新绑定 GridView 中的数据以显示更改。

一个小例子可能是这样的:

<asp:TemplateField HeaderText="Edit">
    <ItemTemplate>
        <asp:Button ID="EditImageButton" runat="server" Text="Edit" CommandName="Edit"' />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:Button ID="UpdateButton" runat="server" ToolTip="Save" CommandName="Update"/>
    </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <ItemTemplate>
        <% #Eval("Status")%>
    </ItemTemplate>
    <EditTemplate>
        <asp:DropdownList ID="List" runat="server" />
    <EdiTemplate>
</asp:TemplateField>


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = Session["domainData"];
        GridView1.DataBind();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int index = GridView1.EditIndex;
    int key = Convert.ToInt32(GridView.DataKeys[index].Value);
    GridViewRow row = GridView1.Rows[index];
    DropdownList = row.FindControl("List") as DropDownList;

    // Save changes to database

    // GridView1.EditIndex = -1;
    // Rebind grid data
}
于 2012-09-11T12:49:02.907 回答
0

假设您有 DropDownList 并且以任何方式绑定它:

<asp:GridView ID="Grid" runat="server" OnRowUpdating="SQL_Update">
  <asp:TemplateField> 
    <EditItemTemplate>
      <asp:Button ID="Btn" runat="server" CommandName="Update" Text="Update" />
    </EditItemTemplate>
  </asp:TemplateField> 
  <asp:TemplateField>
    <EditItemTemplate>
      <asp:DropDownList ID="ddl" runat="server" /> 
    </EditItemTemplate> 
  </asp:TemplateField> 
</asp:GridView>

在您后面的代码中,您应该有一个更新事件,特别是使用GridViewUpdateEventArgs e

protected void SQL_Update(object sender, GridViewUpdateEventArgs e)
{
   /*
     You'll have to find the control in the GridView, using FindControl, and cast it to a DropDownList  
     Using ddl.SelectedValue isn't going to work
   */
   string sql = "Update table set Status = " + ((DropDownList)Grid.Rows[e.RowIndex].FindControl("ddl")).SelectedValue 
   //Other SQL logic here

   //Commit the database changes, using whichever SQL driver you have.
}
于 2012-09-11T13:00:00.867 回答