I have a dropdownlist populated by my database, below is my code:
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsNames"
DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
<asp:SqlDataSource ID="dsNames" runat="server"
ConnectionString="<%$ ConnectionStrings:dbProfilesConnectionString %>"
SelectCommand="SELECT Lname+', '+Fname AS Name,Id FROM tblProfile">
</asp:SqlDataSource>
Beside my Dropdownlist, I have a button and below I have a Gridview, what I want to achieve is whenever I select one of the name in my Dropdownlist, then when I click the button the name should be added in gridview. I am using asp.net with c#.
您可以使用选定的下拉列表名称将新项目添加到您的gridview数据源中,然后将gridview与新数据源绑定。
Hey Try this code.
DataTable dttable = new DataTable();
DataColumn column;
DataRow row;
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "EmpName";
dttable.Columns.Add(column);
row = table.NewRow();
row["EmpName"] = DropDownList1.SelectedText;
dttable.Rows.Add(row);
Gridview1.DataSource = dttable;
Gridview1.DataBind();
Before Adding next record please maintain datatable in Session or View state.And play with that.
Hope it helps you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.