The requirement is as follows:
The value obtained from the database is bound to the gridview. Some values take the value and convert it into text.
For example:
0 merchandisers
1 as a buyer
2. Form reviewer
3. As the Administrator
In the gridview, if data is directly bound without judgment on the value at the business layer, the value is displayed as a number,
We can see that all the values displayed in the factory and role are numerical values. How can we convert them into the text I want?
I first searched the internet, and we all used two methods:
One is to do something in the database and Use Case ...... When
Two formats of the CASE statement:
Syntax format:
Case <Single-value expression>
When <expression value> then <SQL statement or return value>
When <expression value> then <SQL statement or return value>
...
When <expression value> then <SQL statement or return value>
End
Example
SELECT
(CASE RoleValueID
WHEN 01 THEN 'merchandiser'
WHEN 02 THEN 'purchasers'
WHEN 03 THEN 'reviewer'
WHEN 04 THEN 'postmaster'
ELSE 'non-human animal'
END) AS RoleName
FROM UserRole
This method is very common and does not have a great impact on the program, however, if the data type of the constructor corresponding to the entity layer in the program is not a character type (usually int), there will be a problem where the data type does not match.
Another method:
Use the template in the gridview to work with related events in the gridview
<Asp: Button ID = "Button2" runat = "server" CommandArgument = '<% # Eval ("id ") %> 'commandname = "IsPass" Text = '<% # Eval ("ispass "). toString () = "1 "? "Reviewed": "Not reviewed" %>'
/>/// Specify the CommandArgument value as id
E. CommandArgument is the ID of the data corresponding to the currently clicked button, which is written in RowCommand ()
(This code comes from
Tp: // token)
The third method can also be obtained using javascript.
Now, I am using the RowDataBound event as an action. We all know that this event is very helpful. You can change the style of the gridview here. Now I use it like this:
If (e. Row. RowType = DataControlRowType. DataRow)
{
Switch (e. Row. Cells [3]. Text. Trim ())
{
Case "0 ":
E. Row. Cells [3]. Text = "merchandiser ";
Break;
Case "1 ":
E. Row. Cells [3]. Text = "buyer ";
Break;
}
Switch (e. Row. Cells [2]. Text. Trim ())
{
Case "1 ":
E. Row. Cells [2]. Text = "manufacturing a factory ";
Break;
Case "2 ":
E. Row. Cells [2]. Text = "manufacturing plant 2 ";
Break;
}
}
Let's take a look at the comparison results:
After running:
Before running:
In this way, I want to achieve the desired effect. Of course, I have not tried whether this can be used for data operations (I only need to show the layer to achieve the desired effect here)
RowDataBound can also change many items in the gridview.
1. Change the font color
Front-end hyperlink Column
<Asp: HyperLinkField DataNavigateUrlFormatString = "Detail. aspx" Text = "click to View Details">
</Asp: HyperLinkField>
Background:
Protected void gridview1_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. DataRow)
{
HyperLink hlf = (HyperLink) e. Row. Cells [1]. Controls [0];
If (Convert. ToDateTime (gridview1.DataKeys [e. Row. RowIndex]. Value. ToString ()> = DateTime. Now)
{
Hlf. ForeColor = System. Drawing. Color. Red; // change color
}
}
}
2. Determine specific conditions and change the cell background color
Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
... {// Data loading
If (e. Row. RowType = DataControlRowType. DataRow)
... {// Determine whether it is a data row
If (e. Row. Cells [8]. Text = "USA ")
... {// Determine whether the value of column 9th is "USA"
// E. Row. BackColor = System. Drawing. Color. Red;
E. Row. Cells [8]. BackColor = System. Drawing. Color. Red;
}
}
}
3. The parent form calls the gridview of the child form.
In the parent form, call the gridview value of the child form (simple page interaction ):
Parent form code:
Note: Single-host events use window. open to open a new form and obtain the focus <Script language = javascript>...
Function openpage (htmlurl)
...{
Var newwin = window. open (htmlurl, "newWin", "toolbar = no, location = no, directories = no, status = no, scrollbars = yes, menubar = no, resizable = yes, top = 100, left = 200, width = 650, height = 300 ");
Newwin. focus ();
Return false;
}
</Script>
</Head>
<Body>
<Input type = text id = "name"/>
Call in the button:
<Input type = button value = "call" onclick = "return openpage ('gridviewclientclick. aspx ');"/>
</Body>
Subform code:
Note: In girdview, e. Row. Attributes adds the click attribute, ReKey, and transmits the value of the third column of this Row to protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
...{
If (e. Row. RowType = DataControlRowType. DataRow)
...{
E. Row. Attributes. Add ("ondblclick", "ReKey ('" + e. Row. Cells [2]. Text + "')");
// E. Row. Attributes ["style"] = "Cursor: hand ";
//// Keyboard Events
// E. Row. Attributes. Add ("OnKeyDown", "GridViewItemKeyDownEvent ('" + e. Row. Cells [1]. Text + "')");
}
}
(This code comes from: http://blog.csdn.net/mingzhecode/archive/2007/10/08/1814546.aspx)
MSDN:
Http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowdatabound (VS.80). aspx