vs2010网站gridview怎么绑定数据源

 我来答
yj...2@sina.com
2016-12-23 · 超过212用户采纳过TA的回答
知道小有建树答主
回答量:315
采纳率:0%
帮助的人:242万
展开全部
前台: <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="5"
AutoGenerateColumns="False" HeaderStyle-VerticalAlign="Middle" CellPadding="3"
Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
Height="221px" Width="500px" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:BoundField DataField="CID" HeaderText="用户ID" ReadOnly="true">
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="用户姓名" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Sex" HeaderText="性别" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="家庭住址" >
<ItemStyle Width="140px" />
</asp:BoundField>
<asp:BoundField DataField="Post" HeaderText="邮政编码" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True">
<ItemStyle Width="70px" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<PagerSettings Visible="False" />
<FooterStyle Font-Bold="True" />
<HeaderStyle Font-Bold="False" Font-Italic="False" />
</asp:GridView>
后台:
using System;
using System.Data;
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;

using System.Data.SqlClient;

public partial class Update : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=wxd;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//绑定GridView
public void bind()
{
string sqlstr = "select * from Admin";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "Admin");
GridView1.DataSource = myds;
GridView1.DataBind();
}
//编辑行
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
bind();
}
//更新行
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string strsql = "update Admin set Name =@Name ,Sex =@Sex, Address =@Address,Post =@Post where CID = @CID ";
SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
try
{
sqlcmd.Parameters.Add(new SqlParameter("@CID", SqlDbType.Int, 4));
sqlcmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar,50 ));
sqlcmd.Parameters.Add(new SqlParameter("@Sex", SqlDbType.VarChar, 50));
sqlcmd.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar, 50));
sqlcmd.Parameters.Add(new SqlParameter("@Post", SqlDbType.VarChar, 50));

sqlcmd.Parameters["@CID"].Value = GridView1.Rows[e.RowIndex].Cells[0].Text;
sqlcmd.Parameters["@Name"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Sex"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Address"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Post"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();

sqlcmd.Connection.Open();
sqlcmd.ExecuteNonQuery();
this.GridView1.EditIndex = -1;
}
catch (SqlException ex)
{
throw ex;
}
sqlcmd.Connection.Close();
bind();
}
//取消编辑行
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
bind();
}
//设定列宽
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) || e.Row.RowState == DataControlRowState.Edit)
{
for (int i = 1; i < GridView1.Columns.Count-1;i++ )
{

TextBox txt = (TextBox)e.Row.Cells[i].Controls[0];
txt.Width = Unit.Pixel(60);
}
}

if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C9D3E2',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
e.Row.Attributes["style"] = "Cursor:pointer";
}

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式