Fill The Datagrid View In Even Or Odd Rows

In gridview filling in the data in alternate rows refers to even or odd rows. It's an efficient way to fill a custom gridview. I designed a four-column gridview  in the database and I filled in alternate rows in the web form. If the gridview data is present, then fill in any background color, which is used to show if any data is present .

I used the Datareader control to fill in the grid sample code. Here is a method to fill in the data grid.
  1. protected void getDetails() {  
  2.         try {  
  3.             SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnection"].ToString());  
  4.             connection.Open();  
  5.             //SqlDataAdapter adt = new SqlDataAdapter("select * from tbl_data ", connection);  
  6.             //DataTable dt = new DataTable();  
  7.             //adt.Fill(dt);  
  8.             SqlCommand cmd = new SqlCommand("select * from tbl_data ", connection);  
  9.             SqlDataReader sdr = null;  
  10.             sdr = cmd.ExecuteReader();  
  11.             DataTable dt = new DataTable();  
  12.             DataRow dr = null;  
  13.             int i = 0;  
  14.             dt.Columns.Add("line");  
  15.             dt.Columns.Add("maint");  
  16.             dt.Columns.Add("wd");  
  17.             dt.Columns.Add("stores");  
  18.             dt.Columns.Add("quality");  
  19.             if (sdr.HasRows) {  
  20.                 while (sdr.Read()) {  
  21.                     dr = dt.NewRow();  
  22.                     dr["line"] = string.Empty;  
  23.                     dr["maint"] = string.Empty;  
  24.                     dr["wd"] = string.Empty;  
  25.                     dr["stores"] = string.Empty;  
  26.                     dr["quality"] = string.Empty;  
  27.                     dt.Rows.Add(dr);  
  28.                     dr = dt.NewRow();  
  29.                     dr["line"] = sdr["line"].ToString();  
  30.                     dr["maint"] = sdr["maint"].ToString();  
  31.                     dr["wd"] = sdr["wd"].ToString();  
  32.                     dr["stores"] = sdr["stores"].ToString();  
  33.                     dr["quality"] = sdr["quality"].ToString();  
  34.                     dt.Rows.Add(dr);  
  35.                 }  
  36.             }  
  37.             grd_data.DataSource = dt;  
  38.             grd_data.DataBind();  
  39.         } catch (Exception ex) {}  
Also bind the data in ASP control
  1. <asp:GridView ID="grd_data" runat="server" AutoGenerateColumns="false" Width="100%" Height="100%" AlternatingRowStyle-BackColor="#CDFFFF" HeaderStyle-CssClass="myhdr" HeaderStyle-Font-Italic="true" OnRowDataBound="GridDatabound" RowStyle-CssClass="RowStyle" HeaderStyle-HorizontalAlign="Center" GridLines="Both" CssClass="griedline">  
  2.     <HeaderStyle HorizontalAlign="Center" />  
  3.     <Columns>  
  4.         <asp:BoundField HeaderText="LINE" DataField="line" HeaderStyle-HorizontalAlign="Center" ItemStyle-Height="65" ItemStyle-HorizontalAlign="Center" />  
  5.         <asp:BoundField HeaderText="MAINT" DataField="maint" ItemStyle-Height="65" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />  
  6.         <asp:BoundField HeaderText="W D." DataField="wd" ItemStyle-Height="65" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />  
  7.         <asp:BoundField HeaderText="STORE" DataField="stores" ItemStyle-Height="65" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />  
  8.         <asp:BoundField HeaderText="QUALITY" DataField="quality" ItemStyle-Height="65" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" /> </column>  
  9.         </Asp:griedView>  
Ebook Download
View all
Learn
View all