Asp.Net:DataGrid学习(1)

翻译|其它|编辑:郝浩|2004-02-21 14:41:00.000|阅读 1677 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  数据访问是任何实际应用程序的核心部分,而 ASP.NET 提供了一套丰富的控件,这些控件与公共语言运行库中提供的托管数据访问 API 很好地集成在一起。从今天开始我们就来学习DataGrid数据控件的使用。


  为了使页能够访问执行 SQL 数据访问所需的类,必须将 System.Data 和 System.Data.SqlClient 命名空间导入到页中。
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
  若要对 SQL 数据库执行选择查询,请创建与数据库的 SqlConnection,传递连接字符串,然后构造包含查询语句的 SqlCommand 对象,再构造SqlDataReader对象读数据。若要让DataGrid绑定数据,则要把SqlDataReader对象的实例赋予DataGrid的DataSource属性,然后绑定它。
代码如下:


在aspx文件里加入DataGrid控件,修改属性如下:
<h3><font face="宋体">DataGrid 控件的简单选择</font></h3>
<ASP:DataGrid id="DataGrid1" runat="server" Width="700" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="宋体" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" EnableViewState="false">
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
</ASP:DataGrid>


在aspx.cs文件里加入下面代码:
首先导入System.Data.SqlClient命名空间;
再加入下面代码:
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection  myConnection = new SqlConnection("user id=sa;password=;initial catalog=pubs;data source=jeff");
myConnection.Open();   
SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection);
SqlDataReader dr = myCommand.ExecuteReader();
DataGrid1.DataSource=dr;
DataGrid1.DataBind();
myConnection.Close();
}

  这例显示可以如何修改使用从 select HtmlControl 传递的值所选择的数据。SqlDataAdapter 维护一个可用于用值替换变量标识符(由名称前的"@"表示)的 Parameters 集合。在该集合中添加一个指定参数的名称、类型和大小的新 SqlParameter,然后将它的 Value 属性设置为选择的值。


在aspx的html代码里加入下面的代码:
<body MS_POSITIONING="GridLayout">
<form runat="server" ID="Form1">
<h3><font face="宋体">对 DataGrid 控件的参数化选择</font></h3>
选择州:
 <select id="MySelect" runat="server" NAME="MySelect">
 <option selected>CA</option>
 <option>IN</option>
 <option>KS</option>
 <option>MD</option>
 <option>MI</option>
 <option>OR</option>
 <option>TN</option>
 <option>UT</option>
 </select>
<input type="submit" Value="获取作者" runat="server" ID="Submit1" NAME="Submit1"><p>
<ASP:DataGrid id="MyDataGrid" runat="server" Width="700" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="宋体" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" EnableViewState="false" />
</form>
</body>


在aspx.cs文件里加入下面的代码:
private void Submit1_ServerClick(object sender, System.EventArgs e)
{
string strconn="user id=sa;password=;initial catalog=pubs;data source=jeff";
SqlConnection myConnection = new SqlConnection(strconn);
String selectCmd = "select * from Authors where state = @State";
SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, myConnection);


myCommand.SelectCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NVarChar, 2));
myCommand.SelectCommand.Parameters["@State"].Value = MySelect.Value;


DataSet ds = new DataSet();
myCommand.Fill(ds, "作者");


MyDataGrid.DataSource= ds.Tables["作者"].DefaultView;
MyDataGrid.DataBind();
}

  上一例中静态填充选择框的值,但这不太适合那些值在数据库中会更改的情况。因为 select HtmlControl 也支持 IEnumerable


DataSource 属性,可以转而使用选择查询动态填充选择框,这将保证数据库和用户界面始终同步。下面的示例说明此过程。


<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>


<html>
<script language="C#" runat="server">
    SqlConnection myConnection;
    protected void Page_Load(Object Src, EventArgs E)
    {
       myConnection = new SqlConnection("user id=sa;password=;initial catalog=pubs;data source=jeff");       
        if (!IsPostBack)
        {
            SqlDataAdapter myCommand = new SqlDataAdapter("select distinct State from Authors", myConnection);
            DataSet ds = new DataSet();
            myCommand.Fill(ds, "States");
            MySelect.DataSource= ds.Tables["States"].DefaultView;
            MySelect.DataBind();
        }
    }


    public void GetAuthors_Click(Object sender, EventArgs E)
    {
        String selectCmd = "select * from Authors where state = @State";      
        SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, myConnection);
        myCommand.SelectCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NVarChar, 2));
        myCommand.SelectCommand.Parameters["@State"].Value = MySelect.Value;
        DataSet ds = new DataSet();
        myCommand.Fill(ds, "Authors");
        MyDataGrid.DataSource= ds.Tables["Authors"].DefaultView;
        MyDataGrid.DataBind();
    }


</script>


<body style="font: 10.5pt 宋体">
  <form runat="server">
    <h3><font face="宋体">对 DataGrid 控件的动态参数化选择</font></h3>
    选择州:
    <select id="MySelect" DataTextField="State" runat="server"/>
    <input type="submit" OnServerClick="GetAuthors_Click" Value="获取作者" runat="server"/><p>
    <ASP:DataGrid id="MyDataGrid" runat="server"
      Width="700"
      BackColor="#ccccff"
      BorderColor="black"
      ShowFooter="false"
      CellPadding=3
      CellSpacing="0"
      Font-Name="宋体"
      Font-Size="8pt"
      HeaderStyle-BackColor="#aaaadd"
      EnableViewState="false"
    />
  </form>
</body>
</html>


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP