Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Displaying data in datagrid as you need
|
Introduction
Suppose you want to display the student’s details in blue color if they scored above 50 otherwise you want to display in red color. That is in same grid you need to distinguish failed and passed student by changing color or something else.what will you do ? For example u want to concatenate firstname and lastname in dataset to display as full name. let us see my example here.
Example code in c#
<asp:datagrid id="myDataGrid" runat="server" PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NextPrev" OnPageIndexChanged="myDataGrid_PageIndexChanged_1" PageSize="7" AllowPaging="True" Font-Size="X-Small" Font-Names="Tahoma" AutoGenerateColumns="False" CellPadding="4" BackColor="White" BorderColor="#CC9966" BorderWidth="1px" BorderStyle="None"> <FooterStyle forecolor="#330099" backcolor="#FFFFCC"></FooterStyle> <HeaderStyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></HeaderStyle> <PagerStyle horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC" mode="NumericPages"></PagerStyle> <SelectedItemStyle font-bold="True" forecolor="#663399" backcolor="#FFCC66"></SelectedItemStyle> <ItemStyle forecolor="#330099" backcolor="White"></ItemStyle> <Columns> <asp:TemplateColumn HeaderText="Product ID"> <HeaderStyle horizontalalign="Center"></HeaderStyle> <ItemStyle horizontalalign="Center"></ItemStyle> <ItemTemplate> <%= GetIndex() %> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Product Name"> <HeaderStyle horizontalalign="Center"></HeaderStyle> <ItemTemplate> <%# CheckLength((string)DataBinder.Eval(Container.DataItem,"ProductName")) %> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="No Quantity Per Unit?"> <HeaderStyle horizontalalign="Center" verticalalign="Middle"></HeaderStyle> <ItemStyle horizontalalign="Center"></ItemStyle> <ItemTemplate> <asp:Image id="img_QuantityPerUnit" runat="server" ImageUrl='<%# WhichImage((string)DataBinder.Eval(Container.DataItem,"QuantityPerUnit")) %> '/> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Units On Order"> <HeaderStyle horizontalalign="Center"></HeaderStyle> <ItemStyle horizontalalign="Center"></ItemStyle> <ItemTemplate> <%# CheckUnit(DataBinder.Eval(Container.DataItem,"UnitsOnOrder")) %> </ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn DataField="UnitPrice" HeaderText="Unit Price" DataFormatString="${0:N2}"> <HeaderStyle horizontalalign="Center"></HeaderStyle> <ItemStyle horizontalalign="Center"></ItemStyle> </asp:BoundColumn> <asp:TemplateColumn HeaderText="Low Price?"> <HeaderStyle horizontalalign="Center"></HeaderStyle> <ItemStyle horizontalalign="Center"></ItemStyle> <ItemTemplate> <%# CheckPrice(DataBinder.Eval(Container.DataItem,"UnitPrice")) %> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Discontinued?"> <HeaderStyle horizontalalign="Center"></HeaderStyle> <ItemStyle horizontalalign="Center"></ItemStyle> <ItemTemplate> <asp:CheckBox id="chk_Discontinued" RunAt="Server" Checked='<%# DataBinder.Eval(Container.DataItem,"Discontinued") %>'/> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Full Name"> <HeaderStyle horizontalalign="Center"></HeaderStyle> <ItemStyle horizontalalign="Center"></ItemStyle> <ItemTemplate> <%# Change2FullName((string)DataBinder.Eval(Container.DataItem,"FirstName"), (string)DataBinder.Eval(Container.DataItem,"LastName")) %> </ItemTemplate> </asp:TemplateColumn> </Columns>
</asp:datagrid>& <\form>
in server side
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!IsPostBack) { BindGrid(); } } protected int GetIndex() { ItemIndex++; // ---------------------------------------- // Work like an Identity column! // ---------------------------------------- return ((myDataGrid.CurrentPageIndex * myDataGrid.PageSize) + ItemIndex); } private void BindGrid() { string mySQL; // ------------------------------------------------------------------------------- // This select statement have no meaning, I just want to retrieve some data // for the use of example only ;) // ------------------------------------------------------------------------------- mySQL = "SELECT e.LastName,e.FirstName , p.ProductName , p.QuantityPerUnit, p.UnitsOnOrder,p.UnitPrice, p.Discontinued FROM Employees e, Products p"; try { SqlConnection myConnection = new SqlConnection("server=(local);uid=sa;pwd=sa;database=northwind"); SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySQL, myConnection); DataSet myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet); myDataGrid.DataSource = myDataSet; myDataGrid.DataBind(); myConnection.Close(); } catch (SqlException SQLEx) { Label1.Text = SQLEx.Message.ToString(); } catch (Exception ex) { Label1.Text = ex.Message.ToString(); } } protected string Change2FullName(object FirstName, object LastName) { // -------------------------------------------------- // Accept 2 fields from database in Object type, // and then convert them into String type and // concatenate these 2 strings with a space in-between // // Finally, return it to front end // -------------------------------------------------- return (FirstName.ToString() + (" " + LastName.ToString())); } protected string CheckLength(string ProductName) { // -------------------------------------------------- // Concatenate "..." to a ProductName and decrease // its length if its length over 10 characters // -------------------------------------------------- if ((ProductName.ToString().Length > 10)) { return (ProductName.Substring(0, 10) + "..."); } else { return ProductName.ToString(); } } protected string CheckPrice(object UnitPrice) { // -------------------------------------------------- // Accept the input argument, evaluate and then // change its Fore Color on screen // -------------------------------------------------- if ((Convert.ToDouble(UnitPrice) <= 50)) { return ("" + (UnitPrice.ToString() + "")); } else { return ("" + (UnitPrice.ToString() + "")); } } protected string CheckUnit(object UnitsOnOrder) { // ----------------------------------------- // If the UnitsOnOrder returned equals to Zero, // a message of "Out of Stock" will be displayed. // Otherwise, displayed the original value // ----------------------------------------- if ((Convert.ToInt32(UnitsOnOrder) == 0)) { return "Out Of Stock"; } else { return UnitsOnOrder.ToString(); } }
protected string WhichImage(object QuantityPerUnit) { // ----------------------------------------- // Check whether returned value is NULL // // Angry = Null ; Smiley = NOT Null // ----------------------------------------- if ((QuantityPerUnit == DBNull.Value)) { return "~/Images/Angry.gif"; } else { return "~/Images/Smile.gif"; } }
Summary
In this article with example I covered how to paging, how to use templates in Datagrid.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|