如何以格式化方式在gridview中添加下拉列表 [英] How to add dropdownlist in gridview programetically
本文介绍了如何以格式化方式在gridview中添加下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下加载事件:
I have load event as follow:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=testdb;Integrated Security=True;Pooling=False");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from emp",con);
DataSet ds = new DataSet();
da.Fill(ds);
GV_DataByGroupAct.DataSource =ds.Tables[0];
GV_DataByGroupAct.DataBind();
}
查看源视图:
Have look of source view:
<asp:GridView ID="GV_DataByGroupAct" runat="server" AutoGenerateColumns="true"
onrowdatabound="GV_DataByGroupAct_RowDataBound">
<Columns>
</Columns>
</asp:GridView>
和GV_DataByGroupAct_RowDataBound事件:
And GV_DataByGroupAct_RowDataBound event:
protected void GV_DataByGroupAct_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell tc in e.Row.Cells)
{
dl = new DropDownList();
dl.Items.Add("one");
dl.Items.Add("two");
dl.Items.Add("three");
tc.Controls.Add(dl);
}
}
}
当我运行它时,它只向网格视图添加下拉列表控件。
我只是尝试将数据库中的数据以及下拉列表添加到新列中的每一行。请建议。
As i run this it adds only dropdownlist control to the gridview.
I am just trying to add data from database as well as dropdownlist to each rows in a new column. Please suggest.
推荐答案
首先,你应该检查Page_Load上的
。IsPostBack
属性
First of all, you should checkIsPostBack
property onPage_Load
.
protected void Page_Load(object sender, EventArgs e)
{
If(!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=testdb;Integrated Security=True;Pooling=False");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from emp",con);
DataSet ds = new DataSet();
da.Fill(ds);
GV_DataByGroupAct.DataSource =ds.Tables[0];
GV_DataByGroupAct.DataBind();
}
}
在 RowDataBound
中,您正在为所有单元格循环,但是您应该只循环行。
这可能是它在所有单元格上显示的原因。
In RowDataBound
, you are looping for all cells, but you should only loop for the rows.
That may be the reason it is showing on all cells.
做一些事情如下:
我认为唯一的问题是性能下降,不是吗?有人有解决方案吗?
Well do some thing like follow:
Only problem i think would be is performance degrade, isn't? anyone has some solution?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="EmpId" DataSourceID="SqlDataSource1"
ondatabound="GridView1_DataBound">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="True"
SortExpression="EmpId" />
<asp:BoundField HeaderText="Category" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%
ConnectionStrings:DatabaseConnectionString %> ;
SelectCommand = SELECT * FROM [employee] > < / asp:SqlDataSource >
ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT * FROM [employee]"></asp:SqlDataSource>
< br $> b $ b
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
addControl();
}
void addControl()
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DropDownList dl = new DropDownList();
dl.Items.Add("One");
dl.Items.Add("Two");
row.Cells[2].Controls.Add(dl);
}
}
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
addControl();
}
这篇关于如何以格式化方式在gridview中添加下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文