Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  Using ASP.NET DataGrid Web Server Control  »  DataGrid columns creation
 »  Home  »  Web Development  »  ASP.NET  »  Using ASP.NET DataGrid Web Server Control  »  DataGrid columns creation
Using ASP.NET DataGrid Web Server Control
by Misha Zhutov | Published  11/03/2005 | Web Development ASP.NET | Rating:
DataGrid columns creation

DataGrid has different column types which determine the behavior of the columns in the control, such as BoundColumn, ButtonColumn, EditCommandButton, HyperLinkColumn and TemplateColumn.
In this demonstration I used a TemplateColumn type which provides the most freedom in customization. This alloww you to use any controls inside a data row.
TemplateColumn has its own layout tuning and two different set of controls for view and edit mode.
Let’s take a look at the HTML code in the ASPX page that creates the DataGrid columns:

<asp:TemplateColumn ItemStyle-Wrap="true" 
SortExpression="ProductName"
HeaderText="Product Name">

  <ItemTemplate>
    <asp:Label id=Label3 runat="server"
Text='<%# DataBinder.Eval(Container, "DataItem.ProductName") %>'>
</asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
    <asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server"
  ErrorMessage="Product name is empty."
  Display="None"
ControlToValidate="ProductNameTextBox">
Product name is empty.
    </asp:RequiredFieldValidator>



    <asp:TextBox id="ProductNameTextBox" Width="110"
MaxLength="40" runat="server"
Text='<%# DataBinder.Eval(Container, "DataItem.ProductName") %>'>
    </asp:TextBox>
    <asp:Label id="ProductIDLabel" runat="server" Visible=False
Text='<%# DataBinder.Eval(Container, "DataItem.ProductID") %>'>
</asp:Label>
  </EditItemTemplate>
</asp:TemplateColumn>

As we see here, the column has two different control sets – Label for viewing mode and TextBox with a validator for edit mode. Required validator prevents the user from leaving a field empty when editing and will be described below.
Some columns are bound on table columns that have relationships with another tables. For such purposes, I used a DropDownList control in edit mode that bounded on relation table.
Let’s take a look at the HTML code that sets up a DropDownList control in edit mode:

<asp:TemplateColumn SortExpression="CategoryID" HeaderText="Category">
  <ItemTemplate>
    <asp:Label id="Label33" runat="server" Width="150"
Text='<%# GetCategoryName(DataBinder.Eval(Container, _
"DataItem.CategoryID"))%
>'>
    </asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
    <asp:DropDownList id="CategoryDropDownList"
DataSource=<%# Categories%
DataTextField="CategoryName"
DataValueField="CategoryID"
SelectedValue=<%# DataBinder.Eval(Container, "DataItem.CategoryID")%>
Width="150" runat="server">
    </asp:DropDownList>
  </EditItemTemplate>
</asp:TemplateColumn>

After clicking the Edit link, the user is presented with a pre-populated DropDownList, which looks  like this:


Comments    Submit Comment

Comment #1  (Posted by an unknown user on 12/11/2005)
Rating
realy usefull,
 
Comment #2  (Posted by Johana on 12/28/2005)
Rating
El articulo es interante pero muy engorroso, mi pregunta es como podria, hacer que el segundo combo carge con una funcion que reciba 2 valores, el primero q sea resultado del codigo del primer combo y el segundo valor de un campo del grid, eso es lo q me gustaria hacer pero sin tantas clases no se si puedas resolver mi duda.
Gracias
 
Comment #3  (Posted by Misha Zhutov on 12/29/2005)
Rating
My apologies - I can't read Spanish
 
Comment #4  (Posted by an unknown user on 01/06/2006)
Rating
Very useful and demostrates very common operations that are used when programming with the datagrid.
 
Comment #5  (Posted by an unknown user on 01/06/2006)
Rating
Very useful and demostrates very common operations that are used when programming with the datagrid.
 
Comment #6  (Posted by David Wilburn on 01/08/2006)
Rating
Very good article! For a person such as myself that has very little ASP.Net experience, it was very informative.

Much appreciated.
 
Comment #7  (Posted by an unknown user on 03/14/2006)
Rating
hi,
this is very good article.
 
Comment #8  (Posted by AFSUR HUSSAIN on 03/25/2006)
Rating
ya really its fine friends..,


 
Comment #9  (Posted by an unknown user on 04/18/2006)
Rating
good
 
Comment #10  (Posted by an unknown user on 05/19/2006)
Rating
I want to know data update in gtagrid.I can lern lot of thing in your article. Thanks lot for your help.
 
Comment #11  (Posted by an unknown user on 08/10/2006)
Rating
Great but:"The source code (both VB.NET and C#) is attached."...i cannot see it
 
Comment #12  (Posted by Misha Zhutov on 08/11/2006)
Rating
The attachment is on the last article's page, here is direct link on it: http://www.devcity.net/Download.aspx?FileType=Attachment&FileName=WebDataGridExamples.zip&File=3532cd2b-353d-4277-a05e-e6f20d4a268c.zip
 
Sponsored Links