我是新的ASP.NET開發人員並嘗試更新GridView中的一行。我嘗試了許多解決方案,但它不起作用。當我更新一行時,不會顯示錯誤消息,但不會考慮更新。在GridView中更新一行ASP NET不起作用
<asp:GridView ID="grd_quest" Visible = "False" runat="server" CellPadding="4"
DataSourceID="ERP_questionn" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False" DataKeyNames="QUE_id">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField DataField="QUE_id" HeaderText="ID" ReadOnly="True"
SortExpression="QUE_id" ItemStyle-Width="10%" />
<asp:BoundField DataField="QUE_libelle" HeaderText="Libelle"
SortExpression="QUE_libelle" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="ERP_questionn" runat="server"
ConnectionString="<%$ ConnectionStrings:ERPConnectionString %>"
SelectCommand=""
UpdateCommand=
"UPDATE [TR_QUESTION] SET [QUE_libelle] = @QUE_libelle WHERE [QUE_id] = @QUE_id">
<UpdateParameters>
<asp:Parameter Name="QUE_libelle" Type="String" />
<asp:Parameter Name="QUE_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
,因爲它在其他功能變化的SELECT命令,這裏沒有書面清單,所以我寫在C#中SelectCommand
。 我希望這是清楚你
[編輯]
我曾嘗試與此的其他解決方案在C#:
protected void grd_quest_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection sqlConnection1 = new SqlConnection(connectionString);
int id = Convert.ToInt32(grd_quest.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)grd_quest.Rows[e.RowIndex];
//TextBox txtname=(TextBox)gr.cell[].control[];
TextBox textadd = (TextBox)row.Cells[2].Controls[0];
//TextBox textadd = (TextBox)row.FindControl("txtadd");
//TextBox textc = (TextBox)row.FindControl("txtc");
grd_quest.EditIndex = -1;
sqlConnection1.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("UPDATE TR_QUESTION SET QUE_libelle = @p_libelle where QUE_id = @p_id ", sqlConnection1);
cmd.Parameters.AddWithValue("@p_id", id);
cmd.Parameters.AddWithValue("@p_libelle", textadd.Text);
cmd.ExecuteNonQuery();
sqlConnection1.Close();
grd_quest.DataBind();
}
但它不工作過,我有誰要說的錯誤信息即:
數據源'ERP_questionn'不支持更新,除非指定了UpdateCommand。我有嘗試:更新命令=「」,但它不工作太
[編輯新的嘗試]
protected void grd_quest_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = grd_quest.DataKeys[e.RowIndex].Value.ToString();
string libelle = ((TextBox)grd_quest.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
ERP_questionn.UpdateParameters["QUE_id"].DefaultValue = id;
ERP_questionn.UpdateParameters["QUE_libelle"].DefaultValue = libelle;
ERP_questionn.Update();
//ERP_questionn.SelectCommand = DropDownList1.SelectedValue;
}
當我更新行,而不是錯誤消息,但更新不考慮。
[編輯]
也許這可以幫助,在我的GridView我這樣做是爲了顯示我的數據:
protected void lst_facteur_SelectedIndexChanged(object sender, EventArgs e)
{
if (lst_pos.SelectedValue == "1")
{
ERP_questionn.SelectCommand = "select * FROM TR_QUESTION WHERE FAC_id =" + lst_facteur.SelectedValue + " AND QUE_ordreUsine IS NOT NULL";
}
else if (lst_pos.SelectedValue == "2")
{
ERP_questionn.SelectCommand = "select * FROM TR_QUESTION WHERE FAC_id =" + lst_facteur.SelectedValue + " AND QUE_ordreBureau IS NOT NULL";
}
grd_quest.DataBind();
grd_quest.Visible = true;
txt_question.Visible = true;
btn_add_question.Visible = true;
我一直在更新的問題...
我已經添加下面的答案的鏈接使用更新命令 – AsifAli72090