1.GridView内模板列控件绑定,获取数据赋值 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //Girdview中EditItemTemplate中DropDownlist数据的赋值,重点在于IF条件的写法 //e.Row.RowState & DataControlRowState.Edit为判断行状态为EDIT状态时,e.Row.RowType == DataControlRowType.DataRow为判断当前行是数据绑定行 //方法1:增加一个HiddenField 通过HF取得的值传值给DDL //if ((e.Row.RowState & DataControlRowState.Edit) != 0 && e.Row.RowType == DataControlRowType.DataRow) //{ // DropDownList list = ((DropDownList)e.Row.FindControl("dptdepartment")); // HiddenField HF = ((HiddenField)e.Row.FindControl("HiddenFiled1")); // string department = ""; // department = ((HiddenField)e.Row.FindControl("HiddenField1")).Value; // list.SelectedValue = department; // //list.SelectedValue= ((HiddenField)e.Row.FindControl("HiddenFiled1")).Value; //} //方法2:通过结构取值再给DDL赋值 if ((e.Row.RowState & DataControlRowState.Edit) != 0 && e.Row.RowType == DataControlRowType.DataRow) { DropDownList DDLDepartment = ((DropDownList)e.Row.FindControl("dptdepartment")); //DropDownList DDLDepartment = (GridView1.FindControl("dptdepartment") as DropDownList; DropDownList DDLDepartmentName = ((DropDownList)e.Row.FindControl("dptdepartmentname")); string d1, d2; int id = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex]["ID"].ToString()); StaffMeeting meeting = new StaffMeeting(); meeting.Reading_GetDepartmentByID(id, out d1, out d2); DDLDepartment.SelectedValue = d1; DDLDepartmentName.SelectedValue = d2; } } 2.GridView取消更新 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; databind(); } 3.GridView删除操作 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int i = e.RowIndex;//获取GridView索引号 int id = Int32.Parse(GridView1.DataKeys[i].Value.ToString());//获取数据索引号 StaffMeeting meeting = new StaffMeeting(); meeting.Reading_PersonSetOut(id); databind(); } 4.GridView更新操作 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { try { int i = e.RowIndex; GridViewRow gvr = GridView1.Rows[i]; string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); if (gvr.FindControl("EditSeat") as TextBox != null) { string seatno = (gvr.FindControl("EditSeat") as TextBox).Text; string department = (gvr.FindControl("dptdepartment") as DropDownList).SelectedValue; string departmentname = (gvr.FindControl("dptdepartmentname") as DropDownList).SelectedValue; string jobgrade = (gvr.FindControl("EditGrade") as TextBox).Text; StaffMeeting reading = new StaffMeeting(); reading.Reading_PersonUpdate(Convert.ToInt32(id), seatno, department, departmentname, jobgrade); GridView1.EditIndex = -1; databind(); } else { Response.Write("<mce:script language='javascript'><!-- alert('请选择要更新的数据!');window.location.href=window.location.href // --></mce:script>"); } } catch(Exception ex) { throw new Exception(ex.Message, ex); } } 5.GridView判断Command类型 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Edit") { GridView1.Columns[5].Visible = false; GridView1.Columns[6].Visible = true; } if (e.CommandName == "Update" | e.CommandName == "Cancel") { GridView1.Columns[6].Visible = false; GridView1.Columns[5].Visible = true; } } 6.自定义绑定 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[6].Text != "0") { e.Row.BackColor = System.Drawing.Color.Red; e.Row.ForeColor = System.Drawing.Color.White; } if (e.Row.Cells[4].Text == "5") { e.Row.Cells[4].BackColor = System.Drawing.Color.Yellow; } } } 附:控件写法<asp:TemplateField HeaderText="部门"> <ItemTemplate> <%#Eval("department").ToString() %> <%#Eval("DepartmentName").ToString() %> </ItemTemplate> <EditItemTemplate> <asp:DropDownList runat="server" ID="dptdepartment" DataValueField="department" DataSourceID=SqlDataSource1></asp:DropDownList> <asp:HiddenField runat=server ID="HiddenField1" Value='<%#Eval("Department").ToString() %>' /> <asp:DropDownList runat="server" ID="dptdepartmentname" DataValueField="departmentname" DataSourceID=SqlDataSource2></asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="职等"> <ItemTemplate> <%#Eval("jobgrade").ToString() %> </ItemTemplate> <EditItemTemplate> <EditItemTemplate> <asp:TextBox runat=server ID="EditGrade" CssClass="InputCss" Width=62px Text='<%#Bind("JobGrade") %>'></asp:TextBox> </EditItemTemplate> </EditItemTemplate> </asp:TemplateField> <asp:ButtonField ButtonType="Button" ControlStyle-CssClass="ButtonCss" CommandName="Edit" HeaderText="编 辑" ShowHeader=True Text="编辑" > </asp:ButtonField> <asp:TemplateField HeaderText="更新"> <ItemTemplate> <asp:LinkButton runat=server ID="EditBtn" CausesValidation=false CommandName="Update" Text="更新"></asp:LinkButton> <asp:LinkButton runat=server ID="CancelBtn" CausesValidation=false CommandName="Cancel" Text="取消"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="删除"> <ItemTemplate> <asp:ImageButton runat=server ID="DeleteBtn" OnClientClick="return confirm('确定删除?')" CausesValidation=false CommandName="Delete" ImageUrl="~/Images/icon/00.gif" /> </ItemTemplate> </asp:TemplateField>