Daily study summary: datatable sorting by the value of a column, dynamic binding at the gridview background, and page navigation method summary

Source: Internet
Author: User
Document directory
  • 1. Sorting of values in a column in datatable
  • 2. dynamic binding of the gridview background
  • 3. method summary on another page of Navigation
2013-5-311. Sorting of values in a column in a datatable

A problem encountered during development last week is that a column in the datatable is sorted by the value size. Because the datatable is constructed by resplicing other datatable, you need to sort it again. Therefore, the dataview. Sort attribute is used naturally. The Code is as follows:

Dataview DV = DT. defaultview; DV. Sort = "column name DESC"; dt = DV. todatatable ();

The above code is correct, but when constructing a new able, it is constructed using some columns of the existing datatable olddt, as shown below:

Datatable dt = new datatable (); DT. Columns. Add (new column ("column name", olddt. Columns ["oldcolumn"]. GetType ());

Therefore, the results obtained after sorting by dataview. sort are not arranged as expected in the order of values from large to small, but as follows:

1101212304

How can this problem be solved? In fact, you only need to set the type of the column to be sorted to the int type. Maybe the data type you read from the database is the int type, however, when constructing a new able, you must declare the definition again.

The procedure is as follows:

Datatable dt = new datatable (); DT. Columns. Add (new column ("column name", type. GetType ("system. int32 "));

Then, sort by the dataview. Sort attribute to sort by the value of a column.

2. dynamic binding of the gridview background

Sometimes, when you bind a gridview, You need to customize the binding, that is, bind Individual columns, but do not know the specific column name. In this case, you need to use dynamic binding. Next we will introduce how to dynamically bind the backend.

Create the gridviewautobinding. ASPX page,

The front-end code is as follows:

<div>        <asp:GridView ID="Auto_Binding_GridView" runat="server" AutoGenerateColumns="false">        </asp:GridView>       </div>

Only a gridview control is nested in the Div. It is important to set autogeneratecolumns of the gridview to false, so that you can control the specific columns to be displayed. If not set, all columns in the data source are displayed by default.

The background code is as follows:

/// <Summary> // load the page and call gridviewdatabing () method to bind /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> protected void page_load (Object sender, eventargs e) {gridviewdatabing () ;}/// <summary> // obtain the data source and dynamically bind the gridview /// </Summary> protected void gridviewdatabing () {# region reads the database and obtains the data source sqlparameter [] parms = new sqlparameter [1] {New sqlparameter ("@ query", sqldbtype. nvarch Ar, 1000) {value = "" }}; datatable dt = dbhelper. getdatatable ("DBO. get_latesd_log_info ", parms); # endregion # region dynamically binds the gridview auto_binding_gridview.datasource = DT. defaultview; // set the data source auto_binding_gridview.columns.clear () for the datagridview; // remove all columns and dynamically bind var link = new hyperlinkfield () {datatextfield = DT. columns [0]. tostring (), headertext = DT. columns [0]. tostring (), datanavigateurlfields = new stri NG [] {DT. columns [0]. tostring (), DT. columns [1]. tostring ()} And datanavigateurlformatstring = "/2013-5-31/gridviewdetails. aspx? Name = {0} & date = {1} "}; // generates a column similar to a hyperlink and binds the URL of the page to be navigated, that is, querystring parameter auto_binding_gridview.columns.add (Link ); // Add this column to the for (INT I = 1; I <DT. columns. count; I ++) // traverse the remaining columns in the data source, generate the boundfield column, and bind it to the {boundfield BF = new boundfield () {datafield = DT. columns [I]. tostring (), headertext = DT. columns [I]. tostring ()}; auto_binding_gridview.columns.add (BF);} auto_binding_gridview.databind (); // gridview Data Binding # endregion}

 

The result is as follows:

Dynamic binding in the gridview background

Navigate the hyperlinkfield column to another page:

When we bind a gridview to the foreground, there will be many templates. In the above example, we use two templates: boundfield and hyperlinkfield. the display effect of the boundfield template is normal content display, while the Display Effect of hyperlinkfield is similar to a hyperlink. It has several attributes that need special attention:

Headertext: displays the table title. dt. Columns [0]. tostring () is used here ()

Datatextfield: The data item to be bound. dt. Column [0]. tostring ()

Datanavigateurlfields: The field to be bound in querystring. The value of this attribute is a string array. The first and second columns of datatable are used as the fields to be passed in querystring, new String [] {DT. columns [0]. tostring (), DT. columns [1]. tostring ()}

Datanavigateurlformatstring: the URL that you want to navigate to the details page (another page), including querystring, as shown below: "/2013-5-31/gridviewdetails. aspx? Name = {0} & date = {1 }"}

In addition, there are other templates, such as the commandfield module, which can be used to execute command operations.

As follows:

Commandfield cf = new commandfield () {buttontype = buttontype. Link, edittext = "edit", showeditbutton = true}; auto_binding_gridview.columns.add (CF );

In this case, you also need to set the corresponding rowediting event for the gridview. The front-end code is as follows:

<div>        <asp:GridView ID="Auto_Binding_GridView" runat="server"            AutoGenerateColumns="false" onrowediting="Auto_Binding_GridView_RowEditing" >        </asp:GridView>       </div>

Click the properties of the gridview in the design view, click the event setting symbol (lightning symbol), and find the roweditting event double-click. After this operation, A roweditting event is generated as follows:

protected void Auto_Binding_GridView_RowEditing(object sender, GridViewEditEventArgs e)        {            Auto_Binding_GridView.EditIndex = e.NewEditIndex;                 }

You can use the neweditindex attribute of E to obtain the index of the currently edited row.

As follows:

After a commandfield is added

3. method summary on another page of Navigation

The page code is as follows:

<Div id = "Main"> <Table> <tr> <TH> how to navigate to other pages </Th> </tr> <TD> <asp: button id = "Redirect" runat = "server" text = "response. redirect "onclick =" btn1_click "/> </TD> <TD> you can navigate to any page, but you need to perform two" handshakes ", which is slow, parameters can be passed </TD> </tr> <TD> <asp: button id = "btn3" runat = "server" text = "server. transfer "onclick =" btn3_click "/> </TD> <TD> You can only navigate to a site on the site, not cross-site navigation. You only need to perform a handshake, fastest speed </TD> </tr> <TD> <asp: hyperlink id = "hyperlink1" Runat = "server" navigateurl = "~ /2013-5-31/anotherpage. aspx "> hyperlink </ASP: hyperlink> </TD> <TD> server navigation control, only intra-site navigation is allowed </TD> </tr> <TD> <asp: linkbutton id = "linkbutton1" runat = "server" postbackurl = "~ /2013-5-31/anotherpage. aspx "> linkbutton </ASP: linkbutton> </TD> <TD> server-side control, only intra-site navigation is allowed </TD> </tr> <TD> <asp: button id = "btn4" runat = "server" text = "window. location. href "onclick =" btn4_click "/> </TD> <TD> and window. the effect of open is the same. The difference is that the former does not open a new page, but only the navigation </TD> </tr> <TD> <asp: button id = "open" runat = "server" text = "window. open "onclick =" transfer_click "/> </TD> <TD> A new page is opened, and parameters can be passed in the URL, </TD> </tr> </table> </div>

 

Asp.net method summary chart for navigation to other pages

Background code:

protected void btn1_Click(object sender, EventArgs e)        {            Response.Redirect("/2013-5-31/AnotherPage.aspx");        }        protected void Transfer_Click(object sender, EventArgs e)        {            Response.Write("<script language='javascript'>window.open('/2013-5-31/AnotherPage.aspx')</script>");        }         protected void btn3_Click(object sender, EventArgs e)        {            Page.Server.Transfer("/2013-5-31/AnotherPage.aspx");        }        protected void btn4_Click(object sender, EventArgs e)        {            Response.Write("<script language='javascript'>window.location.href='/2013-5-31/AnotherPage.aspx'</script>");        }

 

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.