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.
- protected void getDetails() {
- try {
- SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnection"].ToString());
- connection.Open();
-
-
-
- SqlCommand cmd = new SqlCommand("select * from tbl_data ", connection);
- SqlDataReader sdr = null;
- sdr = cmd.ExecuteReader();
- DataTable dt = new DataTable();
- DataRow dr = null;
- int i = 0;
- dt.Columns.Add("line");
- dt.Columns.Add("maint");
- dt.Columns.Add("wd");
- dt.Columns.Add("stores");
- dt.Columns.Add("quality");
- if (sdr.HasRows) {
- while (sdr.Read()) {
- dr = dt.NewRow();
- dr["line"] = string.Empty;
- dr["maint"] = string.Empty;
- dr["wd"] = string.Empty;
- dr["stores"] = string.Empty;
- dr["quality"] = string.Empty;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["line"] = sdr["line"].ToString();
- dr["maint"] = sdr["maint"].ToString();
- dr["wd"] = sdr["wd"].ToString();
- dr["stores"] = sdr["stores"].ToString();
- dr["quality"] = sdr["quality"].ToString();
- dt.Rows.Add(dr);
- }
- }
- grd_data.DataSource = dt;
- grd_data.DataBind();
- } catch (Exception ex) {}
Also bind the data in ASP control
- <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">
- <HeaderStyle HorizontalAlign="Center" />
- <Columns>
- <asp:BoundField HeaderText="LINE" DataField="line" HeaderStyle-HorizontalAlign="Center" ItemStyle-Height="65" ItemStyle-HorizontalAlign="Center" />
- <asp:BoundField HeaderText="MAINT" DataField="maint" ItemStyle-Height="65" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
- <asp:BoundField HeaderText="W D." DataField="wd" ItemStyle-Height="65" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
- <asp:BoundField HeaderText="STORE" DataField="stores" ItemStyle-Height="65" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
- <asp:BoundField HeaderText="QUALITY" DataField="quality" ItemStyle-Height="65" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" /> </column>
- </Asp:griedView>