C# GridView的OnRowDataBound事件中处于编辑模式的GridView行
以下是如何做到这一点: 在标记中,向C# GridView的OnRowDataBound事件中处于编辑模式的GridView行,c#,asp.net,datagrid,webforms,C#,Asp.net,Datagrid,Webforms,以下是如何做到这一点: 在标记中,向GridView的OnStatBound添加一个方法: <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" ... ... ... OnRowDataBound="GridView1_RowDataBound" > 我让它执行类似于上面的操作,但当我尝试设置SelectedValue=''时
GridView
的OnStatBound添加一个方法:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
... ... ...
OnRowDataBound="GridView1_RowDataBound" >
我让它执行类似于上面的操作,但当我尝试设置SelectedValue=''时,我得到错误:“'selResolutionCategory'有一个SelectedValue,该值无效,因为它不存在于项目列表中。”但我知道它确实存在,通过查询数据库,无法只执行类似于上面的操作(这适用于文本框)据我所知并非如此。有同样的问题。这就是为什么我制作了一个标签-将其visible属性设置为false,然后使用ddlDropDownList.SelectedValue=lblLabel.Text;我厌倦了处理它,知道这会起作用。我如何使用上面的完整代码来实现这一点?我使用绑定字段和模板字段进行编辑。看看下面的我给了你我的asp的完整代码。你必须像我那样使用TemplateField。
protected void gvVehicle_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow gvr in gvVehicleTEMP.Rows)
{
DropDownList ddlLocation = ((DropDownList)gvr.FindControl("ddlLocation"));
Label lblLocationLabel = ((Label)gvr.FindControl("lblLocationLabel"));
conn.Close();
conn.Open();
SqlCommand cmdLocationNames = new SqlCommand("SELECT Name FROM Billers WHERE Store = '" + 1 + "' ORDER BY Name ASC", conn);
List<string> internalLocationsList = new List<string>();
using (SqlDataReader reader = cmdLocationNames.ExecuteReader())
{
while (reader.Read())
{
string interlocations = reader.GetString(0);
internalLocationsList.Add(interlocations);
}
foreach (string locname in internalLocationsList)
{
ddlLocation.Items.Add(new ListItem(locname));
conn.Close();
}
conn.Close();
}
conn.Close();
}
//EDIT: if (lblLocationLabel.Text.Length > 0)
{
ddlLocation.SelectedValue = lblLocationLabel.Text;
}
}
protected void gvVehicle_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow gvr in gvVehicleTEMP.Rows)
{
DropDownList ddlLocation = ((DropDownList)gvr.FindControl("ddlLocation"));
Label lblLocationLabel = ((Label)gvr.FindControl("lblLocationLabel")); //add this line to find the label
conn.Close();
conn.Open();
SqlCommand cmdLocationNames = new SqlCommand("SELECT Name FROM Billers WHERE Store = '" + 1 + "' ORDER BY Name ASC", conn);
List<string> internalLocationsList = new List<string>();
using (SqlDataReader reader = cmdLocationNames.ExecuteReader())
{
while (reader.Read())
{
string interlocations = reader.GetString(0);
internalLocationsList.Add(interlocations);
}
foreach (string locname in internalLocationsList)
{
ddlLocation.Items.Add(new ListItem(locname));
conn.Close();
}
conn.Close();
}
conn.Close();
}
if (lblLocationLabel.Text.Length > 0)//Added if statement
{
ddlLocation.SelectedValue = lblLocationLabel.Text;
}
}
<asp:TemplateField HeaderText="Location" SortExpression="Received">
<ItemTemplate>
<asp:DropDownList ID="ddlLocation" runat="server" AutoPostBack="false">
</asp:DropDownList>
<asp:Label ID="lblLocationLabel" runat="server" Text='<%# Bind("Location") %>' Visible="false"></asp:Label>
<font size="2">Received On: </font>
</ItemTemplate>
<HeaderStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Severity">
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<%# Eval("ResolutionSeverity")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="selResolutionSeverity" runat="server"
DataSourceID="dsSeverities"
DataTextField="Description"
DataValueField="ID"
SelectedValue='<%# Bind("ResolutionSeverity") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
... ... ...
OnRowDataBound="GridView1_RowDataBound" >
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
//Make sure the row is in edit mode to find controls in EditItemTemlates
if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState & DataControlRowState.Edit) > 0))
{
var ddl = GridView1.FindControl("selResolutionSeverity") as DropDownList;
if (ddl != null)
{
// Consider using parameterized query to prevent possible sql injection
string sSQL = @"SELECT ErrorTypeLookupID as Value, ErrorDescription as DisplayText
FROM BabelFish.dbo.ErrorTypeLookup (NOLOCK)
WHERE ErrorType = 'Severity' ";
DataSet DS = new DatabaseAccessing.DatabaseConnection().DS(sSQL);
ddl.DataSource = DS;
ddl.DataTextField = "DisplayText";
ddl.DataValueField = "Value";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("-- Select --", "0"));
}
}
}