The DisplayIndex property of the Container object
has been used to display the record number for each record of the GridView control.
The value of this property starts with 0, hence the necessity of adding 1 to it. Note
that we have taken a TemplateField to use a DropDownList control called drpDept
that will display the department names in the list control. The department to which
a particular employee belongs would be selected by default. To achieve this, we
need to write the following code in the RowDataBound event handler of the
GridView control.
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
Displaying Views of Data (Part I)
[ 146 ]
{
String deptID = ((Label)e.Row.
FindControl("lblDept")).Text;
DataSet ds = new DataSet();
ds = dataManager.GetDepartmentList();
DropDownList ddl =
(DropDownList)e.Row.FindControl("drpDept");
ddl.DataSource = ds.Tables[0];
ddl.DataTextField = "DeptName";
ddl.DataValueField = "DeptCode";
ddl.DataBind();
ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByValue(deptID));
}
}
The department name of each employee is actually stored in a hidden label control
called lblDept. A reference to this control is retrieved using the FindControl()
method.
Pages:
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151