Common Problems and Solutions of the GridView Control

Source: Internet
Author: User

The DataGrid Control in Asp.net 1.1 is used to display data in table format. In Asp.net 2.0, the GridView control is the successor of the DataGrid control, and its functions are much more powerful than that of the DataGrid control, but there are also many differences. The differences found in use are listed below, hoping to help you in your work:

 

1. Processing of hidden fields:

In the DataGrid, we usually put fields that do not need to be displayed and need to be used in programming inDesignThis field is not displayed, but is bound to the data, so we can use the DataGridItem. cell [x]. text to get the data of the required data row.

In the GridView, setting Visible = False directly during the design period cannot meet the above requirements, because the field is not displayed, but the data is not bound to the corresponding Cell, through GridViewRow. cell [x]. all the Text obtained is a Null String. It is estimated that it is a control Bug. If you want to achieve the above effect, you can use other methods, but it is a bit of trouble:

First, do not set the field Visible = False

Then process the RowDataBound event.

Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
E. Row. Cells [x]. Visible = false;
}

 

2. Events related to button columns:

In the DataGrid, to respond to the button-click event in the control button column, you can handle the ItemCommand event: the first parameter of the event, source, is of the object type and its value is DataGrid, the second parameter e is of the DataGridCommandEventArgs type. e contains four attributes: The Item value is the row where the button is located, and the CommandSource value is the clicked button, the value of CommandName is the value defined by the button during the design period. The value of CommandArgument is fixed as a null string. In use, you can identify the button in the row clicked Based on the Item, and distinguish multiple buttons in the row based on the CommandName.

Private void DataGrid1_ItemCommand (object source, DataGridCommandEventArgs e)
{
...
}

In the GridView, you can handle the RowCommand event by responding to the button-click event in the control button column: the first parameter of the event, sender, is of the object type and the value is GridView, the second parameter e is of the GridViewCommandEventArgs type. e contains only three attributes, which are less important than the corresponding events in the DataGrid Control. The value of CommandSource is different from that in the DataGrid, the value of CommandName is the value defined by the button during the design period, and the value of CommandArgument is the index of the row where the button is clicked. In use, you can use CommandArgument to identify the button in the row you are clicking. You can use CommandName to differentiate multiple buttons in the row.

Protected void GridView1_RowCommand (object sender, GridViewCommandEventArgs e)

{

GridViewRow item = this. GridView1.Rows [Convert. ToInt32 (e. CommandArgument)];

}

 

3. Client prompts about buttons:

Generally, we want users to confirm a piece of data before deleting it. You can use the template column to place the button control in the template column and set the client click event for the button.

In Asp.net 1.1, you must use a programming method to specify the client click event for the button. The method is as follows:

<Asp: TemplateColumn>

<ItemTemplate>

<Asp: LinkButton id = "LinkButton1" runat = "server" CausesValidation = "false" CommandName = "Test" Text = "Button"> </asp: LinkButton>

</ItemTemplate>

</Asp: TemplateColumn>

Private void DataGrid1_ItemCreated (object sender, DataGridItemEventArgs e)

{

LinkButton btn = e. Item. FindControl ("LinkButton1") as LinkButton;

If (btn! = Null)

{

Btn. Attributes. Add ("onclick", "return confirm ('Are you sure you want to delete this data? ');");

}

}

In Asp.net 2.0, it is much more convenient. You can set the OnClientClick attribute during the design period to specify the client click event for the button. The method is as follows:

<Asp: TemplateField ShowHeader = "False">

<ItemTemplate>

<Asp: linkButton ID = "LinkButton1" runat = "server" CausesValidation = "false" CommandName = "Test" Text = "button" OnClientClick = "return confirm ('Are you sure you want to delete this data?? '); "> </Asp: LinkButton>

</ItemTemplate>

</Asp: TemplateField>

 

4. formatted output of the time field:

Assume that we have a time field, the data is "10:01:20", and you want to display it"

In the DataGrid, we can simply set the field display format, such:

<Asp: BoundField DataField = "datetime" DataFormatString = "{0: HH: mm}"/>

In the GridView, the same method shows an error and is still displayed as "10:01:20". It is estimated that it is a control Bug (haha, there are quite a few bugs, so many bugs have been released by companies like Microsoft. See...). However, you can use the template column to solve this problem. The method is as follows:

<Asp: TemplateField HeaderText = "time">

<ItemTemplate>

<Asp: Label ID = "Label1" runat = "server" Text = '<% # Bind ("datetime", "{0: HH: mm }") %> '> </asp: Label>

</ItemTemplate>

</Asp: TemplateField>

 

5. Bind null records:

In the DataGrid, We can bind a DataSet or able that contains an empty record to the DataGrid. In this way, the DataGrid displays the field definitions in the data source in the form of a table header.

In the GridView control, however, it is not allowed if DataSet or able is an empty record. The table header is not displayed in the GridView. It can only be implemented through a work und, with a little trouble:

Public void BuildNoRecords (GridView gridView, DataSet ds)

{

If (ds. Tables [0]. Rows. Count = 0)

{

Ds. Tables [0]. Rows. Add (ds. Tables [0]. NewRow ());

GridView. DataSource = ds;

GridView. DataBind ();

Int columnCount = gridView. Rows [0]. Cells. Count;

GridView. Rows [0]. Cells. Clear ();

GridView. Rows [0]. Cells. Add (new TableCell ());

GridView. Rows [0]. Cells [0]. ColumnSpan = columnCount;

GridView. Rows [0]. Cells [0]. Text = "No Records Found .";

}

Else

{

GridView. DataSource = ds;

GridView. DataBind ();

}

}

6. For more information about data binding, use the new data binding mode in GridView.

You can reference this article through this link: http://goforideal.bokee.com/tb. B? DiaryId = 14490605

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.