在 GridView 的下拉列表中编辑和显示数据库值
一篇关于如何在 GridView 列的下拉列表中编辑和显示数据库值的文章。

引言
这篇文章对于使用 GridView 控件的任何人来说都将非常有用。我将尝试解释如何在 GridView 列中添加一个下拉列表来显示和编辑数据库值。DropDownList
将有一个 OnSelectedIndexChanged
事件,用于在无需编辑按钮的情况下更新数据库。该函数背后的代码非常简单,但假定您已经创建了一个填充了字段的 GridView。
背景
这篇文章的背景是为了展示如何在 GridView 内部使用下拉列表。我希望找到一种解决方案,在 GridView 中以下拉列表的形式显示数据库值,然后如何编辑这些值,而无需用户导航到另一个页面,例如使用编辑按钮。下拉列表将包含用户可以从中选择的其他值,但最大的数字不会大于 GridView 中的总行数。例如,如果 GridView 中有 5 行,则下拉列表中将显示数字 1、2、3、4、5,默认数字将是该行的正确数字。
Using the Code
如前所述,代码量相对较少但非常有用。下面提供了注释来解释代码的工作方式。需要注意的主要事项是
ddroworder
是 GridView 中的下拉列表- GridView1 是 GridView
- 下拉列表已在页面开头声明
以下代码应放入 Page_Load
事件中。
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
Int32 countrows = 0;
foreach (GridViewRow row in GridView1.Rows)
{
dropdownlist = ((DropDownList)row.FindControl("ddroworder"));
countrows++;
dropdownlist.DataValueField = countrows.ToString();
dropdownlist.Items.Add(dropdownlist.DataValueField);
//The following code adds the numbers to the dropdownlist until the
//highest number is the same as the total number of rows in the gridview
for (int countallrows = 1; countallrows <= GridView1.Rows.Count;
countallrows++)
{
dropdownlist.Items.Add(new ListItem(countallrows.ToString(),
countallrows.ToString()));
}
}
//The code below applies the correct datakey value as the DropDownList's
//default selected value for that row
for (int x = 0; x < GridView1.Rows.Count; x++)
{
dropdownlist =
((DropDownList)GridView1.Rows[x].FindControl("ddroworder"));
dropdownlist.SelectedValue =
GridView1.DataKeys[x].Values[2].ToString().Trim();
}
}
}
将代码添加到页面加载事件后,下一步是为 OnSelectedIndexChanged
事件创建代码。
public void dd_OnSelectedIndexChanged(object sender, EventArgs e)
{
//This will get the dropdownlist which has been clicked
DropDownList ddl = (DropDownList)sender;
//The row will be the currently selected row from which the dropdownlist
//was clicked
GridViewRow row = (GridViewRow)ddl.NamingContainer;
//Now we know which row is clicked we can get the datakey for that row
string stselectedDatakey =
GridView1.DataKeys[row.RowIndex].Values["Datakey1"].ToString();
//Standard stored procedure can be used to update the database.
//The selected value is the new value to be updated into the database
//stselectedDatakey is the datakey for that row
SqlCommand sqlcommand_ddupdate = new SqlCommand("update_table", con);
sqlcommand_ddupdate.CommandType = CommandType.StoredProcedure;
sqlcommand_ddupdate.Parameters.Add(new SqlParameter("@value1", SqlDbType.Int));
sqlcommand_ddupdate.Parameters["@value1"].Value = ddl.SelectedValue;
sqlcommand_ddupdate.Parameters.Add(new SqlParameter("@value2",
SqlDbType.VarChar));
sqlcommand_ddupdate.Parameters["@value2"].Value = stselectedDatakey;
}
下拉列表
<asp:TemplateField HeaderText="Row Order">
<ItemTemplate>
<asp:DropDownList ID="ddroworder" runat="server"
OnSelectedIndexChanged="dd_OnSelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
关注点
GridView 是一个非常有用的组件,而且我发现在使用 GridView 与其他控件时,解释如何使用的文章并不多。
历史
如果我发现任何其他有用的 GridView 功能,我将在此处添加。祝您使用愉快!