Answered by:
Keep Old Data And Bind New Data In Gridview

Question
-
User2033107836 posted
Hello
This is my table data in sql 2008 table
ItemName Belongs To Qty Apple A 1 Apple C 6 Apple D 2 Mango E 9 Mango R 3 Lemon K 1 Lemon N 3 Lemon J 4 Lemon U 6 My Dropdownlist has following Item
Apple Mango Lemon forexample : Now User Select Mango In Gridivew I Want To Show Like This
Belongs To Qty Mango E 9 R 3 Second Time User Pass Lemon I want to show like this
Belongs To Qty Mango E 9 R 3 Lemon N 3 J 4 U 6 So on if user pass value it has to show new data with old data..
How To Do So Using asp.net 2.0 C#
Thanking You
Friday, May 25, 2018 9:52 AM
Answers
-
User-369506445 posted
hi
you can set true the AutoPostBack in your drop-down list and fill your grid view in OnSelectedIndexChanged
I create a sample that show how do you do it, please first try my sample in a new project and if was correct the change to your needs
in code behind
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Session["GridDataTable"] = null; FillDropDown(); } } protected void ddl_OnSelectedIndexChanged(object sender, EventArgs e) { string text = ddl.SelectedItem.Text; var dt = getSearchItems(text); if (Session["GridDataTable"] == null) { Session["GridDataTable"] = dt; grd.DataSource = dt; grd.DataBind(); return; } DataTable lastDataTable = (DataTable)Session["GridDataTable"]; lastDataTable.Merge(dt); DataTable distinctTable = lastDataTable.DefaultView.ToTable( /*distinct*/ true); Session["GridDataTable"] = distinctTable; grd.DataSource =Session["GridDataTable"]; grd.DataBind(); } void FillDropDown() { const string query = "select distinct [ItemName] from yourTable"; using (var con = new SqlConnection(ConnectionString)) { con.Open(); using (var com = new SqlCommand(query, con)) { SqlDataReader sdrselect = com.ExecuteReader(); ddl.DataTextField = "ItemName"; ddl.DataValueField = "ItemName"; ddl.DataSource = sdrselect; ddl.DataBind(); } } } DataTable getSearchItems(string param) { string query = "select * from yourTable where ItemName='" + param + "'"; using (var con = new SqlConnection(ConnectionString)) { con.Open(); using (var com = new SqlCommand(query, con)) { using (SqlDataReader dr = com.ExecuteReader()) { var tb = new DataTable(); tb.Load(dr); return tb; } } } }
and in Html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head > </head> <body> <form id="form1" runat="server"> <asp:DropDownList runat="server" ID="ddl" AutoPostBack="true" OnSelectedIndexChanged="ddl_OnSelectedIndexChanged" /> <asp:GridView ID="grd" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="ItemName"> <ItemTemplate> <asp:Label ID="lblItemName" runat="server" Text='<%#Eval("ItemName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="BelongsTo"> <ItemTemplate> <asp:Label ID="lblBelongsTo" runat="server" Text='<%#Eval("BelongsTo") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Qty"> <ItemTemplate> <asp:Label ID="lblQty" runat="server" Text='<%#Eval("Qty") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form> </body> </html>
I hope it can be helpful
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 25, 2018 10:49 AM
All replies
-
User-369506445 posted
hi
you can set true the AutoPostBack in your drop-down list and fill your grid view in OnSelectedIndexChanged
I create a sample that show how do you do it, please first try my sample in a new project and if was correct the change to your needs
in code behind
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Session["GridDataTable"] = null; FillDropDown(); } } protected void ddl_OnSelectedIndexChanged(object sender, EventArgs e) { string text = ddl.SelectedItem.Text; var dt = getSearchItems(text); if (Session["GridDataTable"] == null) { Session["GridDataTable"] = dt; grd.DataSource = dt; grd.DataBind(); return; } DataTable lastDataTable = (DataTable)Session["GridDataTable"]; lastDataTable.Merge(dt); DataTable distinctTable = lastDataTable.DefaultView.ToTable( /*distinct*/ true); Session["GridDataTable"] = distinctTable; grd.DataSource =Session["GridDataTable"]; grd.DataBind(); } void FillDropDown() { const string query = "select distinct [ItemName] from yourTable"; using (var con = new SqlConnection(ConnectionString)) { con.Open(); using (var com = new SqlCommand(query, con)) { SqlDataReader sdrselect = com.ExecuteReader(); ddl.DataTextField = "ItemName"; ddl.DataValueField = "ItemName"; ddl.DataSource = sdrselect; ddl.DataBind(); } } } DataTable getSearchItems(string param) { string query = "select * from yourTable where ItemName='" + param + "'"; using (var con = new SqlConnection(ConnectionString)) { con.Open(); using (var com = new SqlCommand(query, con)) { using (SqlDataReader dr = com.ExecuteReader()) { var tb = new DataTable(); tb.Load(dr); return tb; } } } }
and in Html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head > </head> <body> <form id="form1" runat="server"> <asp:DropDownList runat="server" ID="ddl" AutoPostBack="true" OnSelectedIndexChanged="ddl_OnSelectedIndexChanged" /> <asp:GridView ID="grd" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="ItemName"> <ItemTemplate> <asp:Label ID="lblItemName" runat="server" Text='<%#Eval("ItemName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="BelongsTo"> <ItemTemplate> <asp:Label ID="lblBelongsTo" runat="server" Text='<%#Eval("BelongsTo") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Qty"> <ItemTemplate> <asp:Label ID="lblQty" runat="server" Text='<%#Eval("Qty") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form> </body> </html>
I hope it can be helpful
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 25, 2018 10:49 AM -
User2033107836 posted
Hello Vahid
Thanks For Your Code
Thanks Lot
Friday, May 25, 2018 5:27 PM