DataGrid

简介: 2010年7月19日星期一 议程 列表控件概述 列表控件是如何工作的 DataGrid基本使用 DataGrid常用的使用技巧 DataGrid其他使用技巧   列表控件概述 能够取得数据集,并自动遍历它们 比其他控件要复杂得多 ...

2010719星期一

议程

列表控件概述

列表控件是如何工作的

DataGrid基本使用

DataGrid常用的使用技巧

DataGrid其他使用技巧

 

列表控件概述

能够取得数据集,并自动遍历它们

比其他控件要复杂得多

这些控件把绑定到它们的数据通过HTML教程表现出来

常用作其他实际显示数据的控件的容器

功能强大、较复杂

用于以下应用:报表、购物推车、产品列表、查询结果显示、导航菜单

 

列表控件是如何工作的

DataSource属性:最简单地讲,就是一组相同特征的对象或者一个相同对象的集合

Items集合:每一个列表绑定控件都有一个Items集合,集合中的每一个ItemDataSource所指定的一个对象

 

数据绑定

列表控件基于ASP.NET框架,需要你明确地进行数据绑定。这就意味着:只有当DataBind方法被调用时,才真正需要轮询其DataSource所代表的数据。

DataBind方法被调用时,列表绑定控件将轮询DataSource,创建Items集合,并从DataSource取回数据,以初始化Items集合。

 

DataGrid基本使用

DataGrid控件可用于创建各种样式的表格。它还支持对项目的选择和操作,是最复杂、功能最强大!

DataGridAutoGenerateColumns属性缺省是True。当AutoGenerateColumnsTrue时,DataGrid将检查其数据源和其对象映射,并为每一个共有属性或者字段创建一个列。

每一个自动产生的列成为一个BoundColumn(绑定列)。绑定列根据其数据表对应列的数据类型,自动将其转化为一个字符串,显示在表格的一个单元中。

 

DataGrid的使用

Private void dgShow_EditCommand(object source,System.Web.UI.WebControls.DataGridCommandEvent e)

{

dgShow.EditItemIndex=e.Item.ItemIndex;

BindData();

}

Private void dgShow_cancelCommand(object source,System.Web.UI.WebControls. DataGridCommandEvent e)

{

dgShow.EditItemIndex=-1;

BindData();

}

Private void dgShow_PageIndexChanged(object source,System.Web.UI.WebControls. DataGridCommandEvent e)

{

dgShow.CurrentPageIndex=e.NewPageIndex;

BindData();

}

Private void dgShow_UpdateCommand(object source,System.Web.UI.WebControls. DataGridCommandEvent e)

{

 string strStudentID=e.Item.Cells[0].Text;

 string strName=((TextBox)(e.Item.Cells[1].Controls[0])).Text;

 string strPass=((TextBox)(e.Item.Cells[2].Controls[0])).Text;

 string strSex=((CheckBox)(e.Item.Cells[3].FindControl(“cbSex”))).Checked?”1”:”0”;

 string strBirthday=((TextBox)(e.Item.Cells[4].Controls[0])).Text;

 string strEmail=((TextBox)(e.Item.Cells[5].Controls[0])).Text;

 string strSql=”update tbStudentinfo set StudentName=’”+strName+”’,StudentPass=’”+strPass+”’”;

strSql+=”,Sex=”+strSex+”,Birthday=’”+strBirthday+”’,Email=’”+strEmail+”’ where StudentID =’”+ strStudentID +”’”;

ExecuteSql(strSql);

dgShow.EditItemIndex=-1;

BindData();

}

Private void BindData()

{

string strCon=System.Configuration.ConfigurationSettings.AppSettings[“DSN”];

SqlConnection con=new SqlConnection(strCon);

SqlDataAdapter da=new SqlDataAdapter(“select * from tbStudentinfo”,con);

DataSet ds=new DataSet();

Ds.Fill(ds,”studentinfo”);

dgShow.DataSource=ds.Tables[“studentinfo”].DefaultView;

dgShow.DataBind();

foreach(DataGridItem dgi in dgShow.Items)

{

DropDownList ddI=(DropDownList)dgi.FindControl(“ddlSexI”);

If(ddI!=null)

{

bool bSex=(bool)ds.Tables[“studentinfo”].Rows[dgi.ItemIndex][“Sex”];

If(bSex)

ddI.SelectedIndex=0;

else

ddI.SelectedIndex=1;

}

DropDownList ddE=(DropDownList)dgi.FindControl(“ddlSexE”);

If(ddE!=null)

{

bool bSex=(bool)ds.Tables[“studentinfo”].Rows[dgi.ItemIndex][“Sex”];

If(bSex)

ddE.SelectedIndex=0;

else

ddE.SelectedIndex=1;

}

}

}

Private void dgShow_ItemCreated(object sender,System.Web.UI.WebControls. DataGridCommandEvent e)

{

Switch(e.Item.ItemType)

{

Case ListItemType.Item:

Case ListItemType.EditItem:

Case ListItemType.AlternatingItem:

Button myDeleteButton=(Button)e.Item.FindControl(“btnDelete”);

myDeleteButton.Text=”delete this row”;

myDeleteButton.Attributes.Add(“onclick”,”return confirm(‘Do you want to delete the”+e.ItemIndex.To string()+”’”);

break;

}

}

Private void dgShow_ItemCommand(object source,System.Web.UI.WebControls. DataGridCommandEvent e)

{

If(e.CommandName==”UserDelete”)

dgShow_DeleteCommand(source,e);

}

Public void CheckAll(object sender,System.EventArgs e)

{

CheckBox cbAll=(CheckBox)sender;

If(cbAll.Text==”quan xuan”)

{

Foreach(DataGridItem dgi in dgShow.Items)

{

CheckBox cb=(CheckBox)dgi.FindControl(“cbSelect”);

Cb.Checked=cbAll.Checked;

}

}

}

Private void btnDelete_Click(object sender,System.EventArgs e)

{

Foreach(DataGridIem dgi in dgShow.Items)

{

CheckBox cb=(CheckBox)dgi.FindControl(“cbSelect”);

If(cb.Checked)

{

Int nID=int.Parse(dgi.Cells[0].Text);

string strSql=”delete from tbStudentinfo where studentid=’”++”’”;

ExecuteSql(strSql);

}

dgShow.CurrentPageIndex=0;

BindData();

}

}

 

DataGrid常用的使用技巧

日期的显示

传递DataGrid中的值

DataGrid里添加确认删除的对话框

格式化DataGrid:将数据源中的0,1值转换成实际的文字

DataGrid中选择,确认,删除多行复选框列表

利用dropdownlist下拉列表框,显示数据库表中的某个字段

取得DataGrid里的checkbox返回值

DataGrid中加入统计值

 

DataGrid其他使用技巧

如何用程序隐藏和显示DataGrid中的一列

如何控制DataGrid里编辑功能出现的TextBox的宽度?

DataGrid只显示DataSet中多列数据中的一列

DataGrid中的数据导出到Microsoft Excel

 

Private void btnMIME_Click(object sender,System.EventArgs e)

{

Response.ContentType=”application/vnd.ms-excel”;

Response.Charset=””;

This.EnableViewState=false;

System.IO.StringWriter hw=new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw=new System.Web.UI.HtmlTextWriter(sw);

dgShow.RenderControl(hw);

Response.Write(sw.ToString());

Response.End();

}

 

小结

列表控件概述

列表控件是如何工作的

DataGrid基本使用

DataGrid常用的使用技巧

DataGrid其他使用技巧

目录
相关文章
|
7月前
C#-DataGrid的使用(一)
DataGrid的使用
32 0
|
7月前
|
数据可视化
C#-DataGrid的使用(二)
DataGrid的使用
24 0
|
10月前
C#-DataGrid的使用(三)
C#-DataGrid的使用
32 0
|
JSON 数据格式
EasyUI–表格datagrid详解
具体解释都写在文字描述里面了,先看示意图:
307 0
EasyUI–表格datagrid详解
ListView、TreeView和DataGrid。
原文:ListView、TreeView和DataGrid。 1、ListView。 ListView继承自简单的没有特色的ListBox,并使用View属性进行扩展。增加了对基于列显示的支持,并增加了快速切换视图或显示模式的能力,而不需要重新绑定数据以及重新构建列表。
1190 0
|
XML 存储 数据格式
|
.NET 开发框架 存储
|
索引 数据格式 JSON
Datagrid组件的基本讲解
1.datagrid的基本属性 datagrid—- 一种接收后台数据用于,以标准表单的形式展示的组件。
1582 0

热门文章

最新文章