我在使用Gridview时遇到了一个问题。
COL-0返回从< 0,1,..,9>索引的离开类型。在GridView1_RowDataBound()中使用适当的字符串进行映射。但是因为我在下面使用了ItemTemplate,所以RowDataBound()不起作用。
如果我使用BoundField,在显示期间,它很好,但是当我使用GridView1_RowEditing()编辑它时,它再次填充索引值并使其可编辑,这不是所需的解决方案。 COL-0不应该是可编辑的。
我无法解决这个问题。请帮帮我。感谢。
GridView的:
<Columns>
<asp:templatefield headertext="LeaveType">
<itemtemplate>
<%#Eval("LeaveType")%>
</itemtemplate>
<edititemtemplate>
<%#Eval("LeaveType")%>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Balance">
<itemtemplate>
<%#Eval("Balance")%>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="BalanceTxtBox"
text='<%#Eval("Balance")%>'
width="45"
runat="server"/>
</edititemtemplate>
</asp:templatefield>
....
....
<asp:CommandField ShowEditButton="true" HeaderText="Update Record" ItemStyle-Width="30px" />
</Columns>
</asp:GridView>
代码背后:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1Bind();
}
protected void GridView1Bind()
{
....
if (Conn.State.Equals(ConnectionState.Closed))
{
Conn.Open();
SqlCommand cmd = new SqlCommand("SELECT [LeaveType], [Balance] FROM dbo.UserDetails WHERE Username=@user, Conn);
cmd.Parameters.Add("user", SqlDbType.NVarChar).Value = Username;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
catch(Exception)
{
....
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text == "0") ------> [Works for BoundField]
e.Row.Cells[0].Text = "Annual";
else if (e.Row.Cells[0].Text == "1")
e.Row.Cells[0].Text = "Business Trip";
....
}
}