gridview
SqlConnection con;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["orderN"] = "ti";
ViewState["orderT"] = "asc";
bind();
}
}
public void bind()
{
con = new SqlConnection("Data Source=.;Initial Catalog=tt;User ID=sa;Pwd=xxxx");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select id,ti,in,dy from tt",con);
DataSet ds = new DataSet();
da.Fill(ds,"tt");
DataView view = ds.Tables["tt"].DefaultView;
string sort = (string)ViewState["orderN"] + " " + (string)ViewState["orderT"];
view.Sort = sort;
GridView1.DataSource = view;
GridView1.DataKeyNames = new string[] { "id"};
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
con = new SqlConnection("Data Source=.;Initial Catalog=tt;User ID=sa;Pwd=xxxx");
con.Open();
com = new SqlCommand("delete t where id='"+id+"'",con);
com.ExecuteNonQuery();
con.Close();
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ti= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
string in = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
string dy = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
con = new SqlConnection("Data Source=.;Initial Catalog=tt;User ID=sa;Pwd=xxxx");
con.Open();
string upsql = "update t set ti='" + ti + "',in='" + in + "',dy='" + dy + "' where id='"+id+"'";
com = new SqlCommand(upsql,con);
com.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string spage = e.SortExpression;
if ((string)ViewState["orderN"] == spage)
{
if ((string)ViewState["orderT"] == "asc")
{
ViewState["orderT"] = "desc";
}
else
{
ViewState["orderT"] = "asc";
}
}
else {
ViewState["orderN"] = spage;
}
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='red'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onsorting="GridView1_Sorting" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="80%" onrowdatabound="GridView1_RowDataBound"
onpageindexchanging="GridView1_PageIndexChanging">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="id" HeaderText="id" Visible="False" />
<asp:BoundField DataField="ti" SortExpression="ti" HeaderText="aa" />
<asp:BoundField DataField="in" SortExpression="in" HeaderText="bbb" />
<asp:BoundField DataField="dy" SortExpression="dy" HeaderText="cc" />
<asp:CommandField ShowEditButton="True"/>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" OnClientClick="javascript:return confirm('是否确定要删除本记录?')"
CommandName="Delete" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
浙公网安备 33010602011771号