[英]How do I get TextBox in a GridView row when Edit button is clicked if the headers are not databound?
I have a GridView
, and the header fields of the GridView
are the items from a ListBox
in my program. 我有一个
GridView
和的头字段GridView
是从项目ListBox
在我的计划。 Therefore the number of columns generated is dynamic every time I run. 因此,每次运行时,生成的列数都是动态的。 Based on this, if I click the Edit button associated inside my
GridView
, how do I generate a TextBox
for each cell of that row? 基于此,如果我单击
GridView
内关联的“编辑”按钮,如何为该行的每个单元格生成一个TextBox
?
The .cs code I have written is: 我写的.cs代码是:
protected void DONE4_Click(object sender, EventArgs e)
{
Panel7.Visible = true;
SqlDataAdapter mydat = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con);
DataSet dst = new DataSet();
mydat.Fill(dst, "Table");
ListBox3.Items.Clear();
ListBox3.DataSource = dst.Tables[0];
ListBox3.DataTextField = dst.Tables[0].Columns["Profile_Instance"].ColumnName;
ListBox3.DataBind();
int count = ListBox3.Items.Count;
DataTable dt = new DataTable();
DataRow rw = default(DataRow);
for (int i = 0; i < ListBox1.Items.Count; i++)
{
dt.Columns.Add(ListBox1.Items[i].ToString(),
System.Type.GetType("System.String"));
}
for (int j = 0; j < count; j++)
{
rw = dt.NewRow();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
rw[ListBox1.Items[i].ToString()] = " ";
}
dt.Rows.Add(rw);
}
GridView2.DataSource = dt;
GridView2.DataBind();
foreach (GridViewRow grdRow in GridView2.Rows)
{
DropDownList bind_dropdownlist = new DropDownList(); // defining the property of the DropDownList as bind_dropdownlist
bind_dropdownlist = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[0].FindControl("Pro_List")); // finding the DropDownList from the gridiew for binding
SqlDataAdapter mydata = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con);
DataSet dset = new DataSet(); // binding the DropDownList with the dataset ds
mydata.Fill(dset, "Table");
bind_dropdownlist.DataSource = dset;
bind_dropdownlist.DataTextField = "Profile_Instance"; // set the DropDownList's DataTextField as designation which display the designation in the dropdownlist after fetching the data from database
bind_dropdownlist.DataBind();
bind_dropdownlist.Items.Insert(0, new ListItem("---Choose Profile---", "-1"));
}
}
The design code for the GridView
is: GridView
的设计代码为:
<asp:Panel ID="Panel7" runat="server">
<asp:GridView ID="GridView2" runat="server" CellPadding="4"
style="text-align: center; font-size: small" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:DropDownList ID="Pro_List" runat="server">
<asp:ListItem>--Select--</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</asp:Panel>
Can someone please help me on this? 有人可以帮我吗? I hope the question is clear.
我希望问题清楚。
If you are not Binding GridView
and still want to access the controls within it, you can use the GridViewRow
property. 如果您不绑定
GridView
,但仍想访问其中的控件,则可以使用GridViewRow
属性。
protected void grd_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow selectRow = grd.Rows[e.NewEditIndex];
TextBox txtKj=(TextBox)selectRow.Cells[3].FindControl("txtKjId");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.