数据绑定:GridView控件和DataSet
从Java转到.NET了,看了几天的.NET,记点东西吧.
用DataSet绑定GridView控件,很简单,但还是记录一下吧:
1.在aspx页面中拖入GridView控件:
OK,就这样,可以run了.
等会写写如何添加记录的编辑和删除按钮.^^ 楽しいね!
用DataSet绑定GridView控件,很简单,但还是记录一下吧:
1.在aspx页面中拖入GridView控件:
1<asp:GridView ID="grid1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
2</asp:GridView>
2.在对应的.cs文件的Page_Load中:2</asp:GridView>
1protected void Page_Load(object sender, EventArgs e)
2 {
3 conn = new SqlConnection("server=localhost;database=demo;uid=sa;pwd=123456");
4 conn.Open();
5 comm.Connection = conn;
6 comm.CommandText = "select * from demo";
7 SqlDataAdapter adapter1 = new SqlDataAdapter();
8 DataSet set = new DataSet();
9 adapter1.SelectCommand = comm;
10 adapter1.Fill(set);
11 grid1.DataSource = set;
12 grid1.DataBind();
13 }
2 {
3 conn = new SqlConnection("server=localhost;database=demo;uid=sa;pwd=123456");
4 conn.Open();
5 comm.Connection = conn;
6 comm.CommandText = "select * from demo";
7 SqlDataAdapter adapter1 = new SqlDataAdapter();
8 DataSet set = new DataSet();
9 adapter1.SelectCommand = comm;
10 adapter1.Fill(set);
11 grid1.DataSource = set;
12 grid1.DataBind();
13 }
OK,就这样,可以run了.
等会写写如何添加记录的编辑和删除按钮.^^ 楽しいね!