Manoj Mandal

Manoj Mandal

  • 969
  • 450
  • 17.6k

display all data from gridview column

Sep 11 2020 2:25 AM
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3. <title></title>  
  4. </head>  
  5. <body>  
  6. <form id="form1" runat="server">  
  7. <div>  
  8. <table>  
  9. <tr>  
  10. <td>Student ID:</td>  
  11. <td>  
  12. <asp:TextBox ID="txtStID" runat="server"></asp:TextBox>  
  13. </td>  
  14. </tr>  
  15. <tr>  
  16. <td colspan="2" align="center">  
  17. <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />  
  18. </td>  
  19. </tr>  
  20. </table>  
  21. </div>  
  22. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
  23. <div>  
  24. <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"  
  25. GridLines="None" OnRowDataBound = "OnRowDataBound">  
  26. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  27. <EditRowStyle BackColor="#999999" />  
  28. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  29. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  30. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  31. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  32. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  33. <SortedAscendingCellStyle BackColor="#E9E7E2" />  
  34. <SortedAscendingHeaderStyle BackColor="#506C8C" />  
  35. <SortedDescendingCellStyle BackColor="#FFFDF8" />  
  36. <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
  37. </asp:GridView>  
  38. </div>  
  39. </form>  
  40. </body>  
  41. </html>  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. namespace GridViewWithoutDBandStateMngmt  
  9. {  
  10. public partial class WebForm1 : System.Web.UI.Page  
  11. {  
  12. protected void Page_Load(object sender, EventArgs e)  
  13. {  
  14. }  
  15. protected void btnSave_Click(object sender, EventArgs e)  
  16. {  
  17. if (txtStID.Text != string.Empty)  
  18. {  
  19. DataTable dt = new DataTable();  
  20. dt.Columns.Add("Student ID");  
  21. for (int row = 0; row < GridView1.Rows.Count; row++)  
  22. {  
  23. DataRow dr = dt.NewRow();  
  24. dr[0] = GridView1.Rows[row].Cells[0].Text;  
  25. dt.Rows.Add(dr);  
  26. }  
  27. dt.Rows.Add(txtStID.Text);  
  28. GridView1.DataSource = dt;  
  29. GridView1.DataBind();  
  30. txtStID.Text = string.Empty;  
  31. loop();  
  32. }  
  33. }  
  34. protected void OnRowDataBound(object sender, GridViewRowEventArgs e)  
  35. {  
  36. if (e.Row.RowType == DataControlRowType.DataRow)  
  37. {  
  38. foreach (GridViewRow row in GridView1.Rows)  
  39. {  
  40. string name = e.Row.Cells[0].Text;  
  41. ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert""alert('" + name + "');"true);  
  42. }  
  43. }  
  44. }  
  45. }  
  46. }  
but is displaying only current inserted row i want to display all data from the row

Answers (5)