GridView General Usage
1, the GridView Front interface code
The GridView creates columns in two main ways:
1 data binding, which represents the field displayed as text in a data-bound control. DataField = "Answernum", Answernum is a field in the data source. An example is provided:
| The code is as follows |
Copy Code |
<asp:boundfield DataField = "Answernum" > <itemstyle Width = "8%" horizontalalign = "Center"/> </asp:BoundField>
|
2 use template creation, for example:
| The code is as follows |
Copy Code |
<asp:templatefield HeaderText = "View" > <itemtemplate > <asp:linkbutton ID = "linkbuttonviewsoption" runat = "server" CommandName = "viewsoption" commandargument = ' <%# Bind ("qo_id")%> ' > Description </asp:LinkButton> </ItemTemplate> <itemstyle Width = "5%" horizontalalign = "Center"/> </asp:TemplateField>
|
ItemStyle is its template style, adjusted to suit specific requirements.
2. Bound data source
| The code is as follows |
Copy Code |
This.gvQuestions.DataSource = examquestioninfolist; This.gvQuestions.DataBind (); This.gvQuestions.PageIndex = 0; Gvquestions is a GridView control, Examquestioninfolist is a data source, and the GridView data source can be a DataTable or DataSet. |
3, stay in a certain line discoloration
| The code is as follows |
Copy Code |
private void ChangeColor (object sender, GridViewRowEventArgs e) { if (E.row.rowtype = = Datacontrolrowtype.datarow) { E.row.attributes.add ("onmouseover", "C=this.style.backgroundcolor;this.style.backgroundcolor= ' #E6F5FA"); E.row.attributes.add ("onmouseout", "this.style.backgroundcolor=c"); } }
protected void Gvquestions_rowdatabound (object sender, GridViewRowEventArgs e) { ChangeColor (sender, E); } |
4. Operate a line
Give a direct example to illustrate
| The code is as follows |
Copy Code |
| protected void Gvsubjectiveoption_rowcommand (object sender, Gridviewcommandeventargs e) { int rowselected = Convert.ToInt32 (e.commandargument); Questionoptioninfo = Questionoptionbll.getmodel (rowselected); View if (E.commandname = = "Viewsoption") { This.tbOptionStem.Text = questionoptioninfo.qo_option; This.tbCorrectAnswer.Text = questionoptioninfo.qo_subjectanswer;//subjective question answer This.tbCorrectAnswerExplain.Text = Questionoptioninfo.qo_explain; Options attachment string optionaccessorystr = Questionoptioninfo.qo_accessory; string[] Optionaccessoryarr = Optionaccessorystr.split (', '); for (int i = 0; i < optionaccessoryarr.length; i++) { Optionaccessorylist.add (Optionaccessoryarr[i]); } Bindoptionaccessorylist (); } if (E.commandname = = "Deleteoption") { Questionoptionbll.delete (rowselected); int eq_id = questionoptioninfo.eq_id; Bindsubjectiveoption (eq_id)//re-binding subjective problem information } } |
E.commandname some fields corresponding to the foreground interface:
| The code is as follows |
Copy Code |
| <asp: TemplateField HeaderText = "View" <itemtemplate <asp:linkbutton ID = " Linkbuttonviewsoption "runat =" server "CommandName =" viewsoption "commandargument = ' <%# Bind (" qo_id ")%> ' > Description </asp:linkbutton> </itemtemplate> <itemstyle Width = "5%" horizontalalign = "Center"/> </asp:templatefield> <asp:templatefield HeaderText = "Delete" <itemtemplate <asp:imagebutton ID = "ImageButtonDelete2" runat = "server" BorderStyle = "None" CommandName = "deleteoption" commandargument = ' <%# Bind ("qo_id")%> ' ImageUrl = ' ~/images/ Delete.gif "/> </itemtemplate> <itemstyle Width =" 5% " HorizontalAlign = "Center"/> </asp:templatefield> |
where CommandName = "deleteoption" commandargument = ' <%# Bind ("qo_id")%> represents a field in the dataset.
5. Add checkbox and Initialize interface:
| The code is as follows |
Copy Code |
<asp:templatefield > <itemtemplate > <asp:linkbutton ID = "LinkButton1" runat = "server" CommandName = "selectcorrectanswer" commandargument = ' <%# Bind (" qo_id ")%> ' > <asp:checkbox ID = "Cbcorrectanswer" runat = "server"/> </asp:LinkButton> </ItemTemplate>
|
Background logic:
| The code is as follows |
Copy Code |
| <summary> Initializing a CheckBox value </summary> <param name= "GV" >gridview control </param> <param name= "Dtsource" > Data source </param> <param name= "CBName" >checkbox control name </param> <param name= "Cbvalue" >checkbox value </param> private void Initializecheckbox (GridView gv, DataTable dtsource, String cbname, String cbvalue) { int count = DtSource.Rows.Count; if (Count > 0) { for (int i = 0; i < count; i++) { CheckBox cb = GV. Rows[i]. FindControl (cbname) as CheckBox; if (CB!= NULL) { if (Dtsource.rows[i][cbvalue). ToString () = = "0") { Cb. Checked = false; } Else { Cb. Checked = true; } } } } } |
DropDownList General Usage:
1, DropDownList binding simple data source
Write a simple data source here, just to illustrate the effect.
| The code is as follows |
Copy Code |
private void Binddropdownup () { ArrayList al = new ArrayList (); Al. ADD ("11"); Al. ADD ("22"); Al. ADD ("33"); This. Dropdownlist1.datasource = al; This. Dropdownlist1.databind (); } |
Gets the value selected in DropDownList: string text = this. DropDownList1.SelectedItem.Text;
2, DropDownList binding more complex data sources
This is where a dataset is extracted from the database, and a value is displayed in the text box of the Ds,dropdownlist control, where you can get the value of the binding in the background. Specifically as follows:
| The code is as follows |
Copy Code |
private void Binddropdownup () { String strSQL = "SELECT * from [OSCE]." [dbo]. [Questiontype] "; DataSet ds = Query (strSQL);
This. Dropdownlist1.datasource = ds; This. Dropdownlist1.datatextfield = "Qt_name"; This. Dropdownlist1.datavaluefield = "qt_id"; This. Dropdownlist1.databind ()//To bind the data www.111cn.net source to a similar (GridView) control } |
Gets the value of the DropDownList control text box: string text = this. DropDownList1.SelectedItem.Text;
Gets the value value of the DropDownList control binding: String Text2 = this. Dropdownlist1.selectedvalue;
3. Assign values directly to DropDownList when the page is initialized
Digression: This feature is used very much, the implementation is also very simple, but the prerequisite is that you must know. It took me a long time to find out.
| The code is as follows |
Copy Code |
ListItem li = DropDownList1.Items.FindByText ("surgical");//surgery is the value that is wanted to appear, if the DataTextField must have if (Li!= null) { int index = DROPDOWNLIST1.ITEMS.INDEXOF (LI); Dropdownlist1.selectedindex = index; }
|
A simple example illustrates the use of the combination of GridView and DropDownList.
| The code is as follows |
Copy Code |
| <asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False" cellpadding= "4"
Forecolor= "#333333" gridlines= "None" allowsorting= "True" pagesize= "6" >
<footerstyle backcolor= "#990000" font-bold= "True" forecolor= "white"/>
<Columns>
<asp:boundfield datafield= "UserID" headertext= "User ID" readonly= "True"/>
<asp:boundfield datafield= "user_nm" headertext= "User name"/>
<asp:templatefield headertext= "Employee Sex" >
<ItemTemplate>
<asp:dropdownlist id= "DropDownList1" runat= "server" datasource= ' <%# ddlbind ' ()%> '
Datavaluefield= "User_sex" datatextfield= "User_sex" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield= "user_address" headertext= "Home Address"/>
</Columns>
<rowstyle forecolor= "#008066"/>
<selectedrowstyle backcolor= "#669999" font-bold= "True" forecolor= "white"/>
<pagerstyle backcolor= "#006699" font-bold= "True" forecolor= "white"/>
<headerstyle backcolor= "#006699" font-bold= "True" forecolor= "white"/>
</asp:GridView> Using System; Using System.Data; Using System.Data.SqlClient; Using System.Configuration; Using System.Collections; Using System.Web; Using System.Web.Security; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Web.UI.WebControls.WebParts; Using System.Web.UI.HtmlControls; public partial class GridView_DropDownList:System.Web.UI.Page { SqlConnection Sqlcon; String Strcon = "Data source=. Sql2005;database=users; Uid=sa; Pwd=123 "; protected void Page_Load (object sender, EventArgs e)
{
Datatextfiled Gets or sets the data source field that provides text content for a list item
DataValueField Gets or sets the data source field that provides values for each list item
if (! IsPostBack)
{
DropDownList DDL;
String sqlstr = "SELECT * from Users";
Sqlcon = new SqlConnection (Strcon);
SqlDataAdapter Myda = new SqlDataAdapter (Sqlstr, Sqlcon);
DataSet myds = new DataSet ();
Sqlcon. Open ();
Myda. Fill (myDS, "AllUser");
Gridview1.datasource = myds;
Gridview1.databind (); for (int i = 0; I <= gridview1.rows.count-1; i++)
{
DataRowView mydrv = myds. tables["AllUser"]. Defaultview[i];
if (convert.tostring (mydrv["User_sex"]). Trim () = = "Male")
{
DDL = (DropDownList) gridview1.rows[i]. FindControl ("DropDownList1");
Ddl. SelectedIndex = 0;
Ddl. Selecteditem.text = "male";
}
else if (convert.tostring (mydrv["User_sex"]). Trim () = = "female")
{
DDL = (DropDownList) gridview1.rows[i]. FindControl ("DropDownList1");
Ddl. SelectedIndex = 1;
Ddl. SelectedItem. Text = "female";
}
}
Sqlcon. Close ();
}
} Public SqlDataReader Ddlbind () { String sqlstr = "Select DISTINCT user_sex from Users"; Sqlcon = new SqlConnection (Strcon); SqlCommand sqlcom = new SqlCommand (sqlstr, Sqlcon); Sqlcon. Open (); Return sqlcom. ExecuteReader (); } } |
Database: SQl2000 Northwind
The effect is as follows