Asked by:
How to get all values from one column in a gridview

Question
-
User2054357462 posted
I have two gridviews on my page. One that is populated dynamically through a textbox (paste excel data into a textbox, parses said data and then formats that data into a gridview).
The other gridview will only display once the first gridview is populated. I fill the second gridview with data from a sql query, and would like to use each row value from the first gridview in the second gridview. So, the first column of both gridviews should have the exact same data. The second column will be different, as I am going to the insert the data from the second gridview into the database.
My question is how do I get all of the cell values from one column in the first gridview, and fill the second gridview with the exact same data just for that one column.
Tuesday, May 12, 2020 4:26 PM
All replies
-
User1535942433 posted
Hi LiveMahs,
Accroding to your description ,I'm guessing that you want to get the first column of the first gridview.Then you need to check if the database have the same value.If have the same value of the first column ,the second grdiview will be filled the data from database.
Since you don't post your codes,I create a demo.
More details,you could refer to below codes:
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Id"> <ItemTemplate> <asp:TextBox ID="tbId" runat="server" Text='<%# Bind("Id")%>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:TextBox ID="tbname" runat="server" Text='<%# Bind("name") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Num"> <ItemTemplate> <asp:TextBox ID="tbnum" runat="server" Text='<%# Bind("num") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Total"> <ItemTemplate> <asp:TextBox ID="tbtotal" runat="server" Text='<%# Bind("total") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Id"> <ItemTemplate> <asp:TextBox ID="tbId2" runat="server" Text='<%# Bind("Id")%>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:TextBox ID="tbname2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Num"> <ItemTemplate> <asp:TextBox ID="tbnum2" runat="server" Text='<%# Bind("num") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Total"> <ItemTemplate> <asp:TextBox ID="tbtotal2" runat="server" Text='<%# Bind("total") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void bind() { string str, strSql; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); strSql = "select * from Test"; SqlDataAdapter da = new SqlDataAdapter(strSql, str); DataSet ds = new DataSet(); da.Fill(ds, "Test"); this.GridView1.DataSource = ds.Tables[0].DefaultView; this.GridView1.DataBind(); conn.Close(); } public class gvdata { public string Id { get; set; } } protected void Button1_Click(object sender, EventArgs e) { List<string> gvdata = new List<string>(); foreach (GridViewRow row in GridView1.Rows) { string value = ((TextBox)row.FindControl("tbId")).Text; gvdata.Add(value); } if (gvdata.Count > 0) { string x = "("; for (int i = 0; i < gvdata.Count; i++) { x += gvdata[i].ToString(); if (i != gvdata.Count - 1) { x += ","; } } x += ")"; string str, strSql; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); strSql = "select * from Test2 where Id in " + x; SqlCommand cmd2 = new SqlCommand(strSql, conn); conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(cmd2); DataTable dt = new DataTable(); sda.Fill(dt); GridView2.DataSource = dt; GridView2.DataBind(); conn.Close(); } }
Result:
Best regards,
Yijing Sun
Wednesday, May 13, 2020 8:17 AM