我在aspx页面上的gridview代码:
<asp:TemplateField HeaderText="Payments">
<ItemTemplate>
<asp:Label ID="Label31" runat="server" Text='<% #Eval("Payments_Status") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddltype" runat="server" CssClass="form-control" AutoPostBack="false">
<asp:ListItem Value="0" Enabled="false">Select Status</asp:ListItem>
<asp:ListItem Value="Pending">Pending</asp:ListItem>
<asp:ListItem Value="Paid">Paid</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
如果值是&#34;等待&#34;在数据库中,下拉列表应显示为&#34;待定&#34;。与支付相同。
如何在gridview中填充下拉列表在PageLoad中选择selectedValue(链接到屏幕截图)
答案 0 :(得分:0)
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvCustomers.EditIndex == e.Row.RowIndex)
{
DropDownList ddltype = (DropDownList)e.Row.FindControl("ddltype");
string query = "select distinct paymenttype from paymenttypes";
SqlCommand cmd = new SqlCommand(query);
ddltype.DataSource = GetData(cmd);
ddltype.DataTextField = "type";
ddltype.DataValueField = "type";
ddltype.DataBind();
ddltype.Items.FindByValue((e.Row.FindControl("Label31") as Label).Text).Selected = true;
}
}
private DataTable GetData(SqlCommand cmd)
{
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}