Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Displaying data in datagrid as you need
Author: phanindra yerra
Published: 6/29/2006 7:03 AM
Category: C#
Summary: In this article with example I covered how to paging, how to use templates in Datagrid.

 

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 ?

 

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.  

           

 

 [code]

 

<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><asp:label id="Label1" style="Z-INDEX: 102; LEFT: 72px; POSITION: absolute; TOP: 432px" runat="server">Label</asp:label></form>

                Bottom of Form

 

[/code]

 

 

in server side

 

       [code]

      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";

                  }

            }

{/code]

 

In this article with example I covered how to paging, how to use templates in Datagrid.

 

 


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!?



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
online file backup
Color Laser Printer
Gucci sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139