latest Post

Image between GridView rows Asp.Net

The GridView control is the successor to the DataGrid and extends it in a number of ways. In GridView control, you can place images between your gridview rows data. In the previous lesson you already saw how to place blank row between gridview rows.


<span style="font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;">&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt; &amp;lt;html xmlns="http://www.w3.org/1999/xhtml"&amp;gt; &amp;lt;head runat="server"&amp;gt; &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt; &amp;lt;/head&amp;gt; &amp;lt;body&amp;gt; &amp;lt;form id="form1" runat="server"&amp;gt; &amp;lt;div&amp;gt; &amp;lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="400px" onrowdatabound="GridView1_RowDataBound" onrowcreated="GridView1_RowCreated"&amp;gt; &amp;lt;Columns&amp;gt; &amp;lt;asp:BoundField DataField="stor_id" HeaderText="Store ID" /&amp;gt; &amp;lt;asp:BoundField DataField="ord_num" HeaderText="Order No." /&amp;gt; &amp;lt;asp:BoundField DataField="title_id" HeaderText="Title ID" /&amp;gt; &amp;lt;asp:BoundField DataField="qty" HeaderText="Quantity" ItemStyle-HorizontalAlign="Right"/&amp;gt; &amp;lt;/Columns&amp;gt; &amp;lt;/asp:GridView&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/body&amp;gt; &amp;lt;/html&amp;gt; </span>
<span style="font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class _Default : System.Web.UI.Page { int storid = 0; int rowIndex = 1; protected void Page_Load(object sender, EventArgs e) { SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); int i = 0; string sql = null; string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=zen412"; sql = "select distinct top 14 stor_id,ord_num,title_id,qty from sales group by stor_id,ord_num,title_id,qty"; SqlConnection connection = new SqlConnection(connetionString); connection.Open(); SqlCommand command = new SqlCommand(sql, connection); adapter.SelectCommand = command; adapter.Fill(ds); adapter.Dispose(); command.Dispose(); connection.Close(); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { storid = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "stor_id").ToString()); } } protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { bool newRow = false; if ((storid &amp;gt; 0) &amp;amp;&amp;amp; (DataBinder.Eval(e.Row.DataItem, "stor_id") != null)) { if (storid != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "stor_id").ToString())) newRow = true; } if ((storid &amp;gt; 0) &amp;amp;&amp;amp; (DataBinder.Eval(e.Row.DataItem, "stor_id") == null)) { newRow = true; rowIndex = 0; } if (newRow) { AddNewRow(sender, e); } } public void AddNewRow(object sender, GridViewRowEventArgs e) { GridView GridView1 = (GridView)sender; GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert); NewTotalRow.Font.Bold = true; NewTotalRow.BackColor = System.Drawing.Color.Aqua; TableCell HeaderCell = new TableCell(); HeaderCell.Height = 10; HeaderCell.HorizontalAlign = HorizontalAlign.Center ; HeaderCell.ColumnSpan = 4; System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); string imageUrl = "bubble.png"; img.ImageUrl = imageUrl; HeaderCell.Wrap = false; HeaderCell.Controls.Add(img); NewTotalRow.Cells.Add(HeaderCell); GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + rowIndex, NewTotalRow); rowIndex++; } } </span>

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

0 comments:

Post a Comment