第三天ASP.NET学习总结

本文介绍了ASP.NET中DataGrid控件的基本用法,包括显示头部文字、设置样式、排序、分页显示、以及通过大类控制小类等操作。通过实例展示了如何实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.DataGrid控件不用模板时显示头部文字和超链接:
<asp:DataGrid
  ID="dgrdLinks"
  AutoGenerateColumns="False"
  EnableViewState="False"
  CellPadding="10"
  Runat="Server">
<Columns>
  <asp:HyperLinkColumn
    DataNavigateUrlField="link_url"
    DataTextField="link_title"
    HeaderText="链接标题"
    HeaderStyle-Font-Name="Arial"/>
</Columns>
</asp:DataGrid>         //DataGrid默认时显示header,不显示footer的

2.DataGrid的GridLines=none时是没有单元格线的....BackImgeUrl是背景图片的地址.

3.DataGrid的style....同样可以对DataGrid的个别列进行样式设置
<asp:DataGrid
  ID="dgrdTitles"
  CellPadding="10"
  BorderStyle="Dashed"   //虚线框
  BorderColor="blue"
  BorderWidth="8px"
  HeaderStyle-Font-Name="Arial"
  HeaderStyle-Font-Bold="True"
  HeaderStyle-BackColor="lightyellow"
  ItemStyle-Font-Name="Arial"
  ItemStyle-Font-Size="10pt"
  AlternatingItemStyle-BackColor="AliceBlue"
  Runat="Server" />

4.DataGrid对列进行排序(必须将AllowSorting设为True,并要响应一个SortCommand事件)
前台:
     <asp:DataGrid
  ID="dgrdTitles"
  AllowSorting="True"
  OnSortCommand="dgrdTitles_SortCommand"
  AutoGenerateColumns="False"
  CellPadding="10"
  Runat="Server">
<Columns>
  <asp:BoundColumn
    DataField="Title"
    HeaderText="Sort Titles"
    SortExpression="title"/>
  <asp:BoundColumn
    HeaderText="Sort Price"
    DataField="price"
    DataFormatString="{0:c}"
    SortExpression="price"/>
  <asp:BoundColumn
    DataField="Notes" />
</Columns>
</asp:DataGrid>

后台:
     void Page_Load(Object sender , EventArgs e)
{
 if (! IsPostBack ) {
  BindDataGrid( "Title" );
 }
}

void BindDataGrid( string  strSortField ) {
 SqlConnection conPubs;
 SqlCommand cmdSelect;

 conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Pubs" );
 cmdSelect = new SqlCommand( "Select * From Titles Order By " + strSortField, conPubs );
 conPubs.Open();
 dgrdTitles.DataSource = cmdSelect.ExecuteReader();
 dgrdTitles.DataBind();
 conPubs.Close();
}

void dgrdTitles_SortCommand( object s, DataGridSortCommandEventArgs e ) {
 BindDataGrid( e.SortExpression );
}

5.DataGrid对数据进行分页显示(必须将AllowPaging设为True,并要响应一个PageIndexChanged事件)
前台:
<asp:DataGrid
  ID="dgrdProducts"
  AllowPaging="True"
  AllowCustomPaging="True"
  PageSize="3"
  OnPageIndexChanged="dgrdProducts_PageIndexChanged"
  PagerStyle-Mode="NumericPages"
  CellPadding="3"
  Runat="Server" />
后台:
SqlConnection conNorthwind;
string  strSelect;
int intStartIndex;
int intEndIndex;

void Page_Load(Object sender , EventArgs e)
{
 SqlCommand cmdSelect;

 conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
 if (! IsPostBack )
 {
  // Get  Total Pages
  strSelect = "Select Count(*) From Products";
  cmdSelect = new SqlCommand( strSelect, conNorthwind );
  conNorthwind.Open();
  dgrdProducts.VirtualItemCount = ( (int)cmdSelect.ExecuteScalar() / dgrdProducts.PageSize );
  conNorthwind.Close();
  BindDataGrid();
 }
}

void BindDataGrid ()
{
 SqlDataAdapter dadProducts;
 DataSet dstProducts;

 intEndIndex = intStartIndex + dgrdProducts.PageSize;
 strSelect = "Select * From Products Where ProductID > @startIndex And ProductID <= @endIndex Order By ProductID";
 dadProducts = new SqlDataAdapter( strSelect, conNorthwind );
 dadProducts.SelectCommand.Parameters.Add( "@startIndex", intStartIndex );
 dadProducts.SelectCommand.Parameters.Add( "@endIndex", intEndIndex );
 dstProducts = new DataSet();
 dadProducts.Fill( dstProducts );

 dgrdProducts.DataSource = dstProducts;
 dgrdProducts.DataBind();
}

void dgrdProducts_PageIndexChanged( object s, DataGridPageChangedEventArgs e ) {
 intStartIndex = ( e.NewPageIndex * dgrdProducts.PageSize );
 dgrdProducts.CurrentPageIndex = e.NewPageIndex;
 BindDataGrid();
}

6.DataGrid大类控制小类....
前台:
<asp:DataGrid
  ID="dgrdCategories"
  OnItemCommand="dgrdCategories_ItemCommand"
  DataKeyField="CategoryID"
  AutoGenerateColumns="False"
  SelectedItemStyle-BackColor="LightGreen"
  ShowHeader="False"
  Runat="Server">
<Columns>
  <asp:TemplateColumn>
  <ItemTemplate>
  <asp:LinkButton
    Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>'
    Runat="Server"/>
  </ItemTemplate>
  </asp:TemplateColumn>
  <asp:BoundColumn DataField="Description" />
</Columns>
</asp:DataGrid>

<p>

<asp:DataGrid
  ID="dgrdProducts"
  HeaderStyle-BackColor="yellow"
  Runat="Server" />
后台:
void Page_Load(Object sender, EventArgs e)
{
 BindMasterGrid();
}

void BindMasterGrid (){
 SqlConnection conNorthwind;
 SqlCommand cmdSelect;

 conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
 cmdSelect = new SqlCommand( "Select * From Categories", conNorthwind );
 conNorthwind.Open();
 dgrdCategories.DataSource = cmdSelect.ExecuteReader();
 dgrdCategories.DataBind();
 conNorthwind.Close();
}

void BindDetailGrid( int intCatID ) {
 SqlConnection conNorthwind;
 string  strSelect;
 SqlCommand cmdSelect;

 conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
 strSelect = "Select * From Products Where CategoryID=@catID";
 cmdSelect = new SqlCommand( strSelect, conNorthwind );
 cmdSelect.Parameters.Add( "@catID", intCatID );
 conNorthwind.Open();
 dgrdProducts.DataSource = cmdSelect.ExecuteReader();
 dgrdProducts.DataBind();
 conNorthwind.Close();
}

void dgrdCategories_ItemCommand( object s, DataGridCommandEventArgs e ) {
 int intCatID;

 intCatID = Convert.ToInt32(dgrdCategories.DataKeys[ e.Item.ItemIndex ]);
 dgrdCategories.SelectedIndex = e.Item.ItemIndex;
 BindDetailGrid( intCatID );
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值