Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts
This Junk of Code is used to display images on the GridView.

Here I'm creating a static DataTable with some data

The getUserData() Method will creates a DataTable with following fields PictureID,PictureURL, Title, DateAdded.


DataTable GetData()
{

DataTable dt = new DataTable();

// define the table's schema
dt.Columns.Add(new DataColumn("PictureID", typeof(int)));
dt.Columns.Add(new DataColumn("PictureURL", typeof(string)));
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("DateAdded", typeof(DateTime)));

// Create the four records
DataRow dr = dt.NewRow();
dr["PictureID"] = 1;
dr["PictureURL"] = ResolveUrl("~/DisplayingImages/Images/pic001.jpg");
dr["Title"] = "Blue Hills";
dr["DateAdded"] = new DateTime(2005, 1, 15);
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["PictureID"] = 2;
dr["PictureURL"] = ResolveUrl("~/DisplayingImages/Images/pic002.jpg");
dr["Title"] = "Sunset";
dr["DateAdded"] = new DateTime(2005, 1, 21);
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["PictureID"] = 3;
dr["PictureURL"] = ResolveUrl("~/DisplayingImages/Images/pic003.jpg");
dr["Title"] = "Water Lilies";
dr["DateAdded"] = new DateTime(2005, 2, 1);
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["PictureID"] = 4;
dr["PictureURL"] = ResolveUrl("~/DisplayingImages/Images/pic004.jpg");
dr["Title"] = "Winter";
dr["DateAdded"] = new DateTime(2005, 2, 18);
dt.Rows.Add(dr);

return dt;
}





< asp:GridView ID="GridView1" Runat="server" DataSource='< %# GetData() %>' AutoGenerateColumns="False" BorderWidth="1px" BackColor="White" CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC" Font-Names="Arial">
< FooterStyle ForeColor="#000066" BackColor="White">< /FooterStyle>
< PagerStyle ForeColor="#000066" HorizontalAlign="Left" BackColor="White">< /PagerStyle>
< HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#006699">< /HeaderStyle>
< Columns>
< asp:BoundField HeaderText="Picutre ID" DataField="PictureID">
< ItemStyle HorizontalAlign="Center" VerticalAlign="Middle">< /ItemStyle>
< /asp:BoundField>
< asp:BoundField HeaderText="Title" DataField="Title">< /asp:BoundField>
< asp:BoundField HeaderText="Date Added" DataField="DateAdded" DataFormatString="{0:d}">
< ItemStyle HorizontalAlign="Center">< /ItemStyle>
< /asp:BoundField>
< asp:ImageField DataImageUrlField="PictureURL">< /asp:ImageField>
< /Columns>
< SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#669999">< /SelectedRowStyle>
< RowStyle ForeColor="#000066">< /RowStyle>
< /asp:GridView>




--
Enjoy the codeing

Calling javascript in GridView RowCreated Event

//This junk of code is used to call javascript in GridView RowCreated Event

protected void grdIssue_RowCreated(object sender, GridViewRowEventArgs e)
{
CheckBox chkHead = new CheckBox();
if (e.Row.RowType.ToString().Trim() == "Header")
{
chkHead = (CheckBox)e.Row.FindControl("chkbxHeader");
chkHead.Attributes.Add("onClick", "chkBocHeader(" + chkHead.ClientID + ",'H')");
}
else if (e.Row.RowType.ToString().Trim() == "DataRow")
{
CheckBox chkRow = new CheckBox();
chkRow = (CheckBox)e.Row.FindControl("chkboxRow");
chkRow.Attributes.Add("onClick", "chkBocHeader(this,'R')");
}
}

Row Created Example

For this example I've used Northwind as backend and the table is products

Source:


<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" datakeynames="ProductID" datasourceid="SqlDataSource1" cellpadding="4" forecolor="#333333" gridlines="None" onselectedindexchanged="GridView1_SelectedIndexChanged" onrowcreated="GridView1_RowCreated">
<columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName">
<asp:boundfield datafield="CategoryID" headertext="CategoryID" sortexpression="CategoryID">
<asp:boundfield datafield="SupplierID" headertext="SupplierID" sortexpression="SupplierID">
<asp:boundfield datafield="QuantityPerUnit" headertext="QuantityPerUnit" sortexpression="QuantityPerUnit">
<asp:boundfield datafield="UnitPrice" headertext="UnitPrice" sortexpression="UnitPrice">
<asp:boundfield datafield="UnitsOnOrder" headertext="UnitsOnOrder" sortexpression="UnitsOnOrder">
</columns>
<rowstyle backcolor="#EFF3FB">
<footerstyle backcolor="#507CD1" bold="True" forecolor="White">
<pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center">
<selectedrowstyle backcolor="#D1DDF1" bold="True" forecolor="#333333">
<headerstyle backcolor="#507CD1" bold="True" forecolor="White">
<editrowstyle backcolor="#2461BF">
<alternatingrowstyle backcolor="White">
</asp:GridView>
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="">"
SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [SupplierID], [QuantityPerUnit], [UnitPrice], [UnitsOnOrder] FROM [Products]">
</asp:SqlDataSource>


Code behind page :

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Get Price from the row

decimal price;
price = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice"));

//Changing the cell color, by giving a condition

if (price > 50)
{
e.Row.BackColor = System.Drawing.Color.Blue;
e.Row.ForeColor = System.Drawing.Color.White;
e.Row.Font.Bold = true;
}
}
}


cheers
This junk of code about to display populating data on the GridView

Here
I've taken GridView to display the data
SqlDataSource to connect the database
BulletList to populate Territories

The DataBase is NorthWind




< asp:GridView ID="employeesGridView" Runat="server" DataSourceID="employeesDataSource"
AutoGenerateColumns="False" BorderWidth="1px" BackColor="White" GridLines="Horizontal"
CellPadding="3" BorderStyle="None" BorderColor="#E7E7FF" OnRowDataBound="employeesGridView_RowDataBound">
< FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE">< /FooterStyle>
< PagerStyle ForeColor="#4A3C8C" HorizontalAlign="Right" BackColor="#E7E7FF">< /PagerStyle>
< HeaderStyle ForeColor="#F7F7F7" Font-Bold="True" BackColor="#4A3C8C">< /HeaderStyle>
< AlternatingRowStyle BackColor="#F7F7F7">< /AlternatingRowStyle>
< Columns>
< asp:BoundField HeaderText="LastName" DataField="LastName" SortExpression="LastName">< /asp:BoundField>
< asp:BoundField HeaderText="FirstName" DataField="FirstName" SortExpression="FirstName">< /asp:BoundField>
< asp:TemplateField HeaderText="Territories">< ItemTemplate>
< asp:BulletedList ID="bltTerritories" Runat="server" DataTextField="TerritoryDescription"
DataValueField="TerritoryDescription">
< /asp:BulletedList>
< /ItemTemplate>
< /asp:TemplateField>
< /Columns>
< SelectedRowStyle ForeColor="#F7F7F7" Font-Bold="True" BackColor="#738A9C">< /SelectedRowStyle>
< RowStyle ForeColor="#4A3C8C" BackColor="#E7E7FF">< /RowStyle>
< /asp:GridView>

< asp:SqlDataSource ID="employeesDataSource" Runat="server" SelectCommand="SELECT [EmployeeID], [LastName], [FirstName] FROM [Employees] ORDER BY [LastName], [FirstName]"
ConnectionString="< %$ ConnectionStrings:NWConnectionString %>">
< /asp:SqlDataSource>
< asp:SqlDataSource ID="territoriesDataSource" Runat="server" SelectCommand="SELECT dbo.EmployeeTerritories.EmployeeID, dbo.Territories.TerritoryDescription FROM dbo.Territories INNER JOIN dbo.EmployeeTerritories ON dbo.Territories.TerritoryID = dbo.EmployeeTerritories.TerritoryID"
ConnectionString="< %$ ConnectionStrings:NWConnectionString %>">
< /asp:SqlDataSource>

void employeesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
// For each DataRow in the GridView, programmatically access the BulletedList, filter
// the DataView based on the GridView row's EmployeeID value and bind the filtered DataView
// to the BulletedList
if (e.Row.RowType == DataControlRowType.DataRow)
{
BulletedList bl = (BulletedList)e.Row.FindControl("bltTerritories");
territoryData.RowFilter = "EmployeeID = " + ((DataRowView) e.Row.DataItem)["EmployeeID"].ToString();
bl.DataSource = territoryData;
bl.DataBind();
}
}


DataView territoryData; // this DataView will hold all of the Territories, loaded at Page_Load

void Page_Load(object sender, EventArgs e)
{
// Load all of the territories into a DataView from the SqlDataSource
territoryData = (DataView)territoriesDataSource.Select(DataSourceSelectArguments.Empty);
}

--
Cheers
Enjoy the coding