关于Datagrid控件的使用

        以前使用Visual Studio2003的时候,有个Datagrid控件,现在该用Visual Studio2005了,控件也发生了一些改变.现在Datagrid控件现在也摇身变成了Dataview了.不过变了功能更强大了。但是一些基本的东西还是大体一致的。今天我们就来说一下Dataview控件的使用。
        Dataview控件主要是用来实现数据的Select、Insert、Delete、Update功能。现在假定我们存在两个表:基本信息(基本信息包含有ID、姓名、性别、年龄4个字段)、学生成绩(ID、语文、数学、英语 4个字段)。
        首先设置Dataview的AutoGenerateColumns属性为false,并插入以下代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            
<Columns>
                
<asp:BoundField DataField="ID" HeaderText="ID" Visible="false"/>
                
<asp:BoundField DataField="姓名" HeaderText="姓名" />
                
<asp:BoundField DataField="性别" HeaderText="性别" />
                
<asp:BoundField DataField="年龄" HeaderText="年龄" />
                
<asp:BoundField DataField="语文" HeaderText="语文" />
                
<asp:BoundField DataField="英语" HeaderText="英语" />
                
<asp:BoundField DataField="数学" HeaderText="数学" />

            
</Columns>
        
</asp:GridView>
        这样各列就和Dataview绑定在一起了,不过这种绑定还不可以。还要在代码中实现真正的绑定。
private void data_bind()
    
{
        SqlConnection cnn 
= new SqlConnection(ConfigurationManager.ConnectionStrings["ServerConnectionString"].ConnectionString);
        SqlDataAdapter da 
= new SqlDataAdapter("select * from 基本信息 W,学生成绩 S where W.ID=S.ID",cnn);
        DataSet ds 
= new DataSet();
        da.Fill(ds,
"studnet");
    }

1SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerConnectionString"]).ConnectionString;
2        SqlCommand da = new SqlCommand("select * from 基本信息 W,学生成绩  S where W.ID=S.ID ",cnn);
3        GridView1.DataSource = ds.Tables["student"];
4        GridView1.DataBind();
5           Data_Bind();
这样才算真正意义上实现了Dataview的数据绑定。
其中ServerConnectionString是连接字符串名。需要在web.config中插入以下语句.
1<appSettings/>
2    <connectionStrings>
3        <add name="ServerConnectionString" connectionString="Data Source=172.16.1.251;Initial Catalog=qcreport;User ID=sa;Password=fedma13"
4   providerName="System.Data.SqlClient" />
5    </connectionStrings>

好了,对于数据的显示我们已经完成了。下面我们说一下关于排序和分页。
我们知道.net提供了强大的分页和排序功能。我们只需要写两行代码就可以实现排序和分页功能。
        首先设置Dataview的属性AllowPaging 和AllowSorting为true。然后写下如下代码:
 1protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
 2    {
 3        GridView1.PageIndex = e.NewPageIndex;
 4        data_bind();
 5    }

 6    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
 7    {
 8        GridView1.Sorting = e.SortExpression;
 9        data_bind();
10    }

        好了,我们现在就可以对我们写的代码来进行测试了。
posted on 2006-06-09 16:56  Edwin dong  阅读(663)  评论(0)    收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>