完全客制自己的gridview
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True" DataKeyNames="EmployeeID" OnRowCommand="GridView1_RowCommand" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="BtnEdit" runat="server" Text="修改" CommandName="EditRow" ></asp:LinkButton>
<asp:LinkButton ID="BtnDel" runat="server" Text="刪除" CommandName="DeleteRow" CommandArgument='<%# Bind("EmployeeID") %>' OnClientClick="return confirm('確定要刪除嗎?')" ></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="BtnUpdate" runat="server" Text="更新" CommandName="UpdateRow" CommandArgument='<%# Bind("EmployeeID") %>' ValidationGroup="Edit" ></asp:LinkButton>
<asp:LinkButton ID="BtnCancel" runat="server" Text="取消" CommandName="CancelUpdate" ></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmployeeID" InsertVisible="False" SortExpression="EmployeeID">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("EmployeeID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="Add" CommandName="InsertRow" runat="server" ValidationGroup="Insert" >Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="editFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorFirstName" runat="server"
ValidationGroup="Edit" ControlToValidate="editFirstName" Text="*" ForeColor="Red" ErrorMessage="FirstName can not empty"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstName" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorFirstNameAdd" runat="server"
ValidationGroup="Insert" ControlToValidate="txtFirstName" Text="*" ForeColor="Red" ErrorMessage="FirstName can not empty"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="editLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorLastName" runat="server"
ValidationGroup="Edit" ControlToValidate="editLastName" Text="*" ForeColor="Red" ErrorMessage="LastName can not empty"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorLastNameAdd" runat="server"
ValidationGroup="Insert" ControlToValidate="txtLastName" Text="*" ForeColor="Red" ErrorMessage="LastName can not empty"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="editddlCity" runat="server" SelectedValue='<%# Bind("City") %>' >
<asp:ListItem Value="" >請選擇</asp:ListItem>
<asp:ListItem>Seattle</asp:ListItem>
<asp:ListItem>Tacoma</asp:ListItem>
<asp:ListItem>Kirkland</asp:ListItem>
<asp:ListItem>Redmond</asp:ListItem>
<asp:ListItem>London</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorCity" runat="server"
ValidationGroup="Edit" ControlToValidate="editddlCity" Text="*" ForeColor="Red" ErrorMessage="City can not empty"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlCity" runat="server" >
<asp:ListItem Value="" >請選擇</asp:ListItem>
<asp:ListItem>Seattle</asp:ListItem>
<asp:ListItem>Tacoma</asp:ListItem>
<asp:ListItem>Kirkland</asp:ListItem>
<asp:ListItem>Redmond</asp:ListItem>
<asp:ListItem>London</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorCityAdd" runat="server"
ValidationGroup="Insert" ControlToValidate="ddlCity" Text="*" ForeColor="Red" ErrorMessage="City can not empty"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Insert" Text="*" ForeColor="Red" />
<asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="Edit" Text="*" ForeColor="Red" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGridViewData();
}
}
private void BindGridViewData()
{
GridView1.DataSource = EmployeeDataAccessLayer.EmployeeQuery();
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "EditRow")
{
int RowIndex = ((GridViewRow)((LinkButton)(e.CommandSource)).NamingContainer).RowIndex;
GridView1.EditIndex = RowIndex;
BindGridViewData();
}
else if(e.CommandName == "DeleteRow")
{
EmployeeDataAccessLayer.DelEmployee(Convert.ToInt32(e.CommandArgument.ToString()));
BindGridViewData();
}
else if(e.CommandName == "CancelUpdate")
{
GridView1.EditIndex = -1;
BindGridViewData();
}
else if(e.CommandName == "UpdateRow")
{
int EmployeeID = Convert.ToInt32(e.CommandArgument.ToString());
int RowIndex = ((GridViewRow)((LinkButton)(e.CommandSource)).NamingContainer).RowIndex;
string FirstName = ((TextBox)GridView1.Rows[RowIndex].FindControl("editFirstName")).Text;
string LastName = ((TextBox)GridView1.Rows[RowIndex].FindControl("editLastName")).Text;
string City = ((DropDownList)GridView1.Rows[RowIndex].FindControl("editddlCity")).SelectedValue;
EmployeeDataAccessLayer.UpdateEmployee(EmployeeID, FirstName, LastName, City);
GridView1.EditIndex = -1;
BindGridViewData();
}
else if (e.CommandName == "InsertRow")
{
string FirstName = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
string LastName = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
string City = ((DropDownList)GridView1.FooterRow.FindControl("ddlCity")).SelectedValue;
EmployeeDataAccessLayer.InsertEmployee( FirstName, LastName, City);
GridView1.EditIndex = -1;
BindGridViewData();
}
}