Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C从sql数据库中的Dropdownlist更新表中选择值#_C#_Asp.net_Sql_Visual Studio 2010_Sql Server 2008 - Fatal编程技术网

C# 使用C从sql数据库中的Dropdownlist更新表中选择值#

C# 使用C从sql数据库中的Dropdownlist更新表中选择值#,c#,asp.net,sql,visual-studio-2010,sql-server-2008,C#,Asp.net,Sql,Visual Studio 2010,Sql Server 2008,我将Dropdownlist放在GridView1中,Dropdownlist的值为0和1。我想要的是:根据从dropDownlist中选择的值,在我的sql数据库中用该值更新列[Status] protected void SQL_Update(object sender, GridViewUpdateEventArgs e){ SqlConnection conn = new SqlConnection(); conn.ConnectionString = @"Data Source=.\

我将Dropdownlist放在GridView1中,Dropdownlist的值为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();}

如果我是你,从你提供的信息来看,我认为解决办法是

<asp:TemplateField />
这将触发GridView的编辑事件,用于标识正在编辑和设置的行的索引

GridView1.EditIndex = e.NewEditIndex
现在,在该行中,您将拥有值为0和1的DropDownList。您还需要在EditTemplate中有一个按钮,用于保存更改

CommandName="Update"
然后处理网格的RowUpdate事件,以在触发RowUpdate时从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
}


假设您拥有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>

你说“里面”是什么意思?在“状态”列的编辑模板内?在“项目模板”中“确定”。我写这个。现在如何调用SQL\u更新?我还需要在DropDownList2\u SelectedIndexChanged或Page\u load中写些什么?编辑了我的答案。请注意,GridView中有一个“OnRowUpdating”部分。该按钮有一个命令名“Update”,它将触发相应的事件。在SelectedIndexChanged或Page_Load中应该不需要写入任何内容,除非在调用update命令之前需要对DropDownList进行修改。我不知道哪里出错,但当我从DropDownList中选择值时,不要更新表。:/当我从dropdownlist中选择值来更新表中的值(列状态)时,不需要我按钮。我编辑我的帖子,你能告诉我在SQL_更新中哪里出错吗?还要记住,更新数据库后,要显示更改,你必须重新绑定gridview,使用最初用于绑定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
}
<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>
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.
}