Hi @BeUnique,
Is it possible to load values in gridview from Datatable which is getting from dropdown values....?
If you need to implement this requirement, you only need to define the SelectedIndexChanged event of the DropDownList
control, get the value
and text
of the selected option in this event, add it to the DataTable
based on certain judgment conditions, and finally bind the data source of the Gridview
or ListView
you mentioned.
This is a simple demonstration, which may lack some conditional judgments. You need to add or delete them according to your needs:
Index.aspx
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="DDL1"
OnSelectedIndexChanged="DDL1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="ProdName" Value="ProdId"></asp:ListItem>
<asp:ListItem Text="ToothPaste" Value="101"></asp:ListItem>
<asp:ListItem Text="Detergent" Value="102"></asp:ListItem>
<asp:ListItem Text="Choco" Value="103"></asp:ListItem>
<asp:ListItem Text="MicroPaper" Value="104"></asp:ListItem>
</asp:DropDownList>
<hr />
<asp:GridView ID="GV1" runat="server">
</asp:GridView>
</div>
</form>
Index.aspx.cs
public partial class Index : System.Web.UI.Page
{
private static DataTable dt;
private static bool DataBaseIsEmpty;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = getDataResource();
GridViewBind();
}
}
private void GridViewBind()
{
GV1.DataSource = dt;
GV1.DataBind();
}
private DataTable getDataResource()
{
dt = new DataTable();
//get resource from SQL DataBase, just for test below
dt.Columns.Add("ProdId", typeof(int));
dt.Columns.Add("ProdName", typeof(string));
//dt.Rows.Add(105,"ProdName2");
//dt.Rows.Add(106,"ProdName3");
//dt.Rows.Add(107,"ProdName4");
if (dt.Rows.Count == 0) {
DataBaseIsEmpty = true;
}else
{
DataBaseIsEmpty = false;
}
return dt;
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DDL1.SelectedIndex != 0)
{
int value = int.Parse(DDL1.SelectedItem.Value);
string text = DDL1.SelectedItem.Text;
if (DataBaseIsEmpty)
{
dt.Rows.Add(value, text);
}
GridViewBind();
}
}
}
Or if you have other needs, please clarify more details.
Best Regards,
Xudong Peng
If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.