ASP.NET DataGrid questions

  1. What is datagrid? The DataGrid Web server control is a powerful tool for displaying information from a data source. It is easy to use; you can display editable data in a professional-looking grid by setting only a few properties. At the same time, the grid has a sophisticated object model that provides you with great flexibility in how you display the data.

  2. What’s the difference between the System.Web.UI.WebControls.DataGrid and and System.Windows.Forms.DataGrid? The Web UI control does not inherently support master-detail data structures. As with other Web server controls, it does not support two-way data binding. If you want to update data, you must write code to do this yourself. You can only edit one row at a time. It does not inherently support sorting, although it raises events you can handle in order to sort the grid contents. You can bind the Web Forms DataGrid to any object that supports the IEnumerable interface. The Web Forms DataGrid control supports paging. It is easy to customize the appearance and layout of the Web Forms DataGrid control as compared to the Windows Forms one.
  3. How do you customize the column content inside the datagrid? If you want to customize the content of a column, make the column a template column. Template columns work like item templates in the DataList or Repeater control, except that you are defining the layout of a column rather than a row.
  4. How do you apply specific formatting to the data inside the cells? You cannot specify formatting for columns generated when the grid’s AutoGenerateColumns property is set to true, only for bound or template columns. To format, set the column’s DataFormatString property to a string-formatting expression suitable for the data type of the data you are formatting.
  5. How do you hide the columns? One way to have columns appear dynamically is to create them at design time, and then to hide or show them as needed. You can do this by setting a column’s Visible property.
  6. How do you display an editable drop-down list? Displaying a drop-down list requires a template column in the grid. Typically, the ItemTemplate contains a control such as a data-bound Label control to show the current value of a field in the record. You then add a drop-down list to the EditItemTemplate. In Visual Studio, you can add a template column in the Property builder for the grid, and then use standard template editing to remove the default TextBox control from the EditItemTemplate and drag a DropDownList control into it instead. Alternatively, you can add the template column in HTML view. After you have created the template column with the drop-down list in it, there are two tasks. The first is to populate the list. The second is to preselect the appropriate item in the list — for example, if a book’s genre is set to “fiction,” when the drop-down list displays, you often want “fiction” to be preselected.
  7. How do you check whether the row data has been changed? The definitive way to determine whether a row has been dirtied is to handle the changed event for the controls in a row. For example, if your grid row contains a TextBox control, you can respond to the control’s TextChanged event. Similarly, for check boxes, you can respond to a CheckedChanged event. In the handler for these events, you maintain a list of the rows to be updated. Generally, the best strategy is to track the primary keys of the affected rows. For example, you can maintain an ArrayList object that contains the primary keys of the rows to update.

This is just a brief on dealing with ASP.NET DataGrid control. The full version of the document and the sample code is available on MSDN.

This entry was posted in .NET. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

8 Comments on ASP.NET DataGrid questions

  1. vw_pora
    Posted 6/1/2005 at 6:59 pm | Permalink

    #7 You can also check for a row change by using the RowState property.

  2. Posted 9/13/2005 at 4:32 am | Permalink

    How can i send data grid content to email using asp.net

  3. Posted 11/29/2005 at 7:48 am | Permalink

    How do I use the FindControl() function to preselect an item in my dropdownlist?

  4. elain
    Posted 3/2/2006 at 4:37 am | Permalink

    How do I use the FindControl() function to preselect an item in my dropdownlist?

    in DataGrid_ItemDataBound event handler, for example

    void Grid_ItemDataBound (Object sender, DataGridCommandEventArgs e)
    {
    DropDownList ddl = (DropDownList)e.Item.FindControl(”ddl”);
    //preselect an item in ddl
    }

  5. Ekano
    Posted 1/22/2007 at 9:29 pm | Permalink

    How to hide a Datagrid-Itemtemplate (None autogenerate) from code behind page?

  6. Nirbhay Singh
    Posted 3/21/2007 at 2:54 am | Permalink

    it is good but here on your site there should be some facility available to send the problem so that any one can get the answer to specific problem like i have
    there is a DataGrid control on DataGrid control one DropDownList Box is taken now want to use the Dorpdownlist1_selecteditemindexchaner event as it is taken on DataGrid so i can not track this event directly resion is DropDownList control is in DataGrid so directly i can not track it how to track the event of DropDownList in this situation

  7. Hemendra
    Posted 4/24/2007 at 6:26 am | Permalink

    I have Two DropDownList inside DataGrid or GridView,
    Now on Change of selection of First DropDownList, I want to fill value in second DropDownList

  8. Nilofer shaikh
    Posted 1/17/2008 at 1:56 am | Permalink

    If you want to fill the second dropdown then you will have to handle the event of first dropdown selectedindexchanged (Autopostback=true), in that assign the datasource to the second dropdownlist……

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*