Answered by:
ASP.NET Webforms and Gridview, Display in a new page

Question
-
User2114489497 posted
I have a Gridview with 10 columns
On page 1 I only want to display 3 of them and when you click the select link, it should show all 10 columns on page 2.
My problem that I am having is that when I only have 3 visible columns on page 1, then only those 3 will be displayed on page 2, but when I have all 10 visible or displaying on page 1 then all 10 columns for that specific row gets displayed on page 2. Is there a way to do what I want to achieve?
For some extra Info: Its for a car bid college project so its showing the CarMake, CarModel and the Car Image, so when I click on select, all 10 columns should be on page 2.
Any help would be appreciated and sorry if the question is duplicated.
Tuesday, May 28, 2019 8:47 AM
Answers
-
User288213138 posted
Hi Dreyer,
According to your description, I made a demo for you as a reference.
if you want to show three columns, e.g. id1,id2 and id3.
you can add the <Columns> tag inside the GridView tag and add up the columns of your choice.
The code:
The code: WebForm1: <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Id1" HeaderText="Id1" /> <asp:BoundField DataField="Id2" HeaderText="Id2" /> <asp:BoundField DataField="Id3" HeaderText="Id3" /> </Columns> </asp:GridView> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">select link</asp:LinkButton> </div> public string s = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string query = "select * from Test2"; using(SqlConnection con=new SqlConnection(s)) { using(SqlDataAdapter adapter=new SqlDataAdapter(query, con)) { DataTable dt = new DataTable(); adapter.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } protected void LinkButton1_Click(object sender, EventArgs e) { Response.Redirect("WebForm2.aspx"); } Web Form2: <div> <asp:GridView ID="GridView1" runat="server"></asp:GridView> </div> public string s = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string query = "select * from Test2"; using (SqlConnection con = new SqlConnection(s)) { using (SqlDataAdapter adapter = new SqlDataAdapter(query, con)) { DataTable dt = new DataTable(); adapter.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } }
The result:
Best Regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 28, 2019 10:26 AM
All replies
-
User288213138 posted
Hi Dreyer,
According to your description, I made a demo for you as a reference.
if you want to show three columns, e.g. id1,id2 and id3.
you can add the <Columns> tag inside the GridView tag and add up the columns of your choice.
The code:
The code: WebForm1: <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Id1" HeaderText="Id1" /> <asp:BoundField DataField="Id2" HeaderText="Id2" /> <asp:BoundField DataField="Id3" HeaderText="Id3" /> </Columns> </asp:GridView> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">select link</asp:LinkButton> </div> public string s = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string query = "select * from Test2"; using(SqlConnection con=new SqlConnection(s)) { using(SqlDataAdapter adapter=new SqlDataAdapter(query, con)) { DataTable dt = new DataTable(); adapter.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } protected void LinkButton1_Click(object sender, EventArgs e) { Response.Redirect("WebForm2.aspx"); } Web Form2: <div> <asp:GridView ID="GridView1" runat="server"></asp:GridView> </div> public string s = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string query = "select * from Test2"; using (SqlConnection con = new SqlConnection(s)) { using (SqlDataAdapter adapter = new SqlDataAdapter(query, con)) { DataTable dt = new DataTable(); adapter.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } }
The result:
Best Regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 28, 2019 10:26 AM -
User2114489497 posted
Hi Sam
Thank you so much
Tuesday, May 28, 2019 12:16 PM