2012-09-11 37 views
0

我將Dropdownlist放入GridView1中,下拉列表的值爲0和1.我想要的是:根據從dropDownlist中選擇的值更新列[Status]惠普在我的SQL數據庫中的值?在使用C#的sql數據庫的Dropdownlist更新表中選擇的值使用C#

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();} 
+0

當你說'裏面'是什麼意思?在狀態列的EditTemplate裏面? – chead23

+0

項目模板http://postimage.org/image/57losotad/ – user1408956

回答

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. 
} 
+0

好的。我寫這個。現在如何調用SQL_Update?還有什麼我需要在DropDownList2_SelectedIndexChanged或Page_load中寫入? – user1408956

+0

編輯我的答案。請注意,GridView中有一個'OnRowUpdating'部分。該按鈕有一個命令名稱'更新',它應該觸發相應的事件。 除非在調用update命令之前需要對DropDownList進行修改,否則應該不需要在SelectedIndexChanged或Page_Load中寫入任何內容。 – Gobbledigook

+0

我不知道我在哪裏犯錯,但是當我從下拉列表中選擇值時,不更新表格。 :/並不需要我按鈕,只有當我從下拉列表中選擇值來更新表中的值時(列狀態)。 – user1408956

0

如果我是你,從你所提供的信息,我認爲解決辦法是有一個

<asp:TemplateField /> 

列與在該

<EditTemplate> 

將DropDownList本專欄。然後,您需要在GridView某種按鈕的其中有

CommandName="Edit" 

這將觸發你用它來識別正在編輯的行的索引和設置在GridView的編輯事件

GridView1.EditIndex = e.NewEditIndex 

現在該行內你會有值爲0和1,您還需要在您的EditTemplate有一個按鈕,將更改保存與

CommandName="Update" 

你個DropDownList的在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 
} 
相關問題