Gridview_rowdatabound Common methods

Source: Internet
Author: User

1. This does not have to say, the mouse over the line color changes

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{

if (E.row.rowtype = = Datacontrolrowtype.datarow)//Determines whether the current row belongs to a DataRow type row
{
Save the background color of the current row and give it a color when the mouse is on it.
E.row.attributes.add ("onmouseover", "currentcolor=this.style.backgroundcolor;this.style.backgroundcolor=" # 7f9edb ', this.style.fontweight= '; ");
Restores the background color to the previous color when the mouse is left
E.row.attributes.add ("onmouseout", "this.style.backgroundcolor=currentcolor,this.style.fontweight="; ");
}

}

2. This is also very EZ

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{

Switch (e.row.cells[1]. Text.tostring ())
{
Case "1":
E.ROW.CELLS[1]. Text = "franchise Driver";
Break
Case "2":
E.ROW.CELLS[1]. Text = "Company Driver";
Break
}
}

}

3. Read the data source field in DataKeyNames when the line is bound

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
    {
       if (E.row.rowtype = = Datacontrolrowtype.datarow)
         {
            DataKey DataKey = Gridview1.datakeys[e.row.rowindex];
           //string Daa_del = datakey["Daa_del"]. ToString ();

            if (datakey["Foc_del"). ToString () = = "True")
            {
                 E.row.cells[1]. Enabled = false;
                e.row.cells[2]. Enabled = false;
           }
            Else
             {
                 E.row.cells[1]. Enabled = true;
                e.row.cells[2]. Enabled = true;
           }

}

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)    {       if (E.row.rowtype = = Datacontrolrowtype.datarow)        {            e.row.attributes.add ("onclick", "Javascript:alert" (' Current ID: "+ DataBinder.Eval (E.row.dataitem, "CID") + "')");        }    }

What I do is click on the current line to get the value of the specified field for that row! The CID is the ID of the field that the desired value is.

The GridView is a powerful data display control in ASP. RowDataBound event gives us a convenient way to control row and column data.

To get a data column for the current row, I summarize the following methods in practice:

1. cells[x]. Txt.
Gets from the text value of the column cell. This method is simple and high-rate, most commonly used, but the function is simple. There are several drawbacks to this approach:
(1) Unable to get the value of the data column that has the hidden property set, the value is "" (empty).
(2) You can only get data columns that have been defined in the HTML, and you cannot query all the field columns of the current data row in the data source. In general, the number of field columns that are set by the GridView in HTML is often less than the actual number of fields in the data source, because from a business logic perspective, not all fields are to be displayed on the page. When you need to use a field that is not displayed, this method does not directly satisfy the requirement.

2. E.row.cells[x]. FindControl ("Yourcontrolname").
This is to find a server control within a cell to get its data value. This way you can manipulate server controls within a cell. This method is typically used to work with data or controls in a template column.

3. (DataRowView) E.row.dataitem). ROW.ITEMARRAY[X]. ToString ().
The core of this law is E. Row.dataitem, which is the row dataset of the GridView, is the object type, and after converting it to the DataRowView type, you can get more ways to do it. This dataset represents all the field columns of the current row of the data source, Itemarray[x] is an array object for all field columns in the current row, and can be obtained by index X for any field value. The strong point of this method is that all fields of the data source can be queried.

4. DataBinder.Eval (E.row.dataitem, "Yourdatafieldname"). ToString ().
This method is still based on E. Row.dataitem, which is characterized by faster access to any field value of the data source, since the use of DataBinder.Eval (), it is important to note that you do not write a typo name.

5. Place E. Row.dataitem is converted to a custom type to enable data typing.
For example:
Dsrequesttracking.tb_requesttrackingrow rtrow= (Dsrequesttracking.tb_requesttrackingrow) ((DataRowView) E. Row.dataitem). Row;
Requeststatusdropdownlist1.selectedvalue = Rtrow. Statusid.tostring ();
Where Dsrequesttracking is a strongly typed dataset that is customized in the project through the Dsrequesttracking.xsd file, Tb_requesttrackingrow is the way to create a data row object that is automatically generated for this strong type. Through this method, the wide application of typed data is realized. The benefits of data typing are many, and it's obvious to pass a type of data to a method, instead of a bunch of parameters, and there will never be a misspelling of the field name.

Gridview_rowdatabound Common methods

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.