﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-飘着的叶子，什么时候才落地-随笔分类-datagrid</title><link>http://www.cnitblog.com/piaoye12345/category/1031.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 29 Sep 2011 23:20:10 GMT</lastBuildDate><pubDate>Thu, 29 Sep 2011 23:20:10 GMT</pubDate><ttl>60</ttl><item><title>导出excel文件</title><link>http://www.cnitblog.com/piaoye12345/archive/2005/10/26/3586.html</link><dc:creator>萌芽的叶子</dc:creator><author>萌芽的叶子</author><pubDate>Wed, 26 Oct 2005 08:09:00 GMT</pubDate><guid>http://www.cnitblog.com/piaoye12345/archive/2005/10/26/3586.html</guid><wfw:comment>http://www.cnitblog.com/piaoye12345/comments/3586.html</wfw:comment><comments>http://www.cnitblog.com/piaoye12345/archive/2005/10/26/3586.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/piaoye12345/comments/commentRss/3586.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/piaoye12345/services/trackbacks/3586.html</trackback:ping><description><![CDATA[Response.Clear()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Buffer = True<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Charset = "GB2312"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim st As Integer = DateDiff(DateInterval.WeekOfYear, CDate(Session("Weekreport").ToString.Substring(0, 4) + "/1/1"), Session("Weekreport")) + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim filename As String = "attachment;filename=WeekReportBM" + st.ToString + ".xls"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.AppendHeader("Content-Disposition", filename)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312") '//设置输出流为简体中文<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.ContentType = "application/ms-excel" '//设置输出文件类型为excel文件。 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Me.EnableViewState = False<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim myCItrad As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("ZH-CN", True)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter(myCItrad)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Me.Panel1.RenderControl(oHtmlTextWriter)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(oStringWriter.ToString)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.End()<img src ="http://www.cnitblog.com/piaoye12345/aggbug/3586.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/piaoye12345/" target="_blank">萌芽的叶子</a> 2005-10-26 16:09 <a href="http://www.cnitblog.com/piaoye12345/archive/2005/10/26/3586.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>DataGrid删除确认及Item颜色交替</title><link>http://www.cnitblog.com/piaoye12345/archive/2005/10/17/3353.html</link><dc:creator>萌芽的叶子</dc:creator><author>萌芽的叶子</author><pubDate>Mon, 17 Oct 2005 06:13:00 GMT</pubDate><guid>http://www.cnitblog.com/piaoye12345/archive/2005/10/17/3353.html</guid><wfw:comment>http://www.cnitblog.com/piaoye12345/comments/3353.html</wfw:comment><comments>http://www.cnitblog.com/piaoye12345/archive/2005/10/17/3353.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/piaoye12345/comments/commentRss/3353.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/piaoye12345/services/trackbacks/3353.html</trackback:ping><description><![CDATA[<P>&nbsp;private void dg_ItemDataBound(object sender, DataGridItemEventArgs e) <BR>&nbsp;&nbsp;{ <BR>&nbsp;&nbsp;&nbsp;if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) <BR>&nbsp;&nbsp;&nbsp;{ <BR>&nbsp;//删除确认&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;LinkButton delBttn = (LinkButton) e.Item.Cells[1].Controls[0]; <BR>&nbsp;&nbsp;&nbsp;&nbsp;delBttn.Attributes.Add("onclick","javascript:return confirm('确定删除" + e.Item.Cells[4].Text + "?');");&nbsp;<BR>&nbsp;//颜色交替&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='seashell'"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;if(e.Item.ItemType == ListItemType.Item) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;} </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;if(e.Item.ItemType ==ListItemType.AlternatingItem) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='seashell'"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;} <BR>&nbsp;&nbsp;&nbsp;} <BR>&nbsp;&nbsp;}<BR><BR>vb代码为：<BR>&nbsp;&nbsp; If e.Item.ItemType &lt;&gt; ListItemType.Header And e.Item.ItemType &lt;&gt; ListItemType.Footer And e.Item.ItemType &lt;&gt; ListItemType.Separator Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.Item.Attributes("onmouseover") = "this.bgColor='#61AAF5';"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.Item.Attributes("onmouseout") = "this.bgColor='#ffffff';"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If</P><img src ="http://www.cnitblog.com/piaoye12345/aggbug/3353.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/piaoye12345/" target="_blank">萌芽的叶子</a> 2005-10-17 14:13 <a href="http://www.cnitblog.com/piaoye12345/archive/2005/10/17/3353.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>管理 ASP.NET DataGrid 数据列 </title><link>http://www.cnitblog.com/piaoye12345/archive/2005/10/17/3352.html</link><dc:creator>萌芽的叶子</dc:creator><author>萌芽的叶子</author><pubDate>Mon, 17 Oct 2005 06:05:00 GMT</pubDate><guid>http://www.cnitblog.com/piaoye12345/archive/2005/10/17/3352.html</guid><wfw:comment>http://www.cnitblog.com/piaoye12345/comments/3352.html</wfw:comment><comments>http://www.cnitblog.com/piaoye12345/archive/2005/10/17/3352.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/piaoye12345/comments/commentRss/3352.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/piaoye12345/services/trackbacks/3352.html</trackback:ping><description><![CDATA[在ASP.NET的DataGrid数据显示控件编程中，我们有几种方式可以增加DataGrid columns。其中最常见的方法是在web forms设计器中增加，通过在控件工具箱中拖访DataGrid控件到web设计页面，然后在属性生成器中增加Columns列;还有一种方式就是在HTML视图模式下更改HTML代码的方式增加Columns列。但是这两种方式都是在设计时进行的，一旦设计完成就无法更改。其实我们也可以在程序运行时动态的增加或者删除Columns列。在这篇文章中，我将向大家介绍如何编程实现在运行时动态的增加和删除Columns列，其实是通过隐藏或者现实Columns列来实现的。 
<P></P>
<P>　　DataGrid 的Columns属性是访问datagrid Columns的关键所在。访问这个属性返回的是DataGridColumnCollection这样的一个集合对象，它包含了所有的DataGrigColumn对象。DataGridColumnCollection提供了增加一个DataGrigColumn对象和删除一个已经存在的DataGrigColumn对象的方法。，我们将使用DataGridColumnCollection的Add方法来增加一个DataGrigColumn对象，从而在运行时动态的增加一列到DataGrid中去。一个DataGrigColumn代表DataGrid的一列，DataGrid的Visible属性用来显示或者隐藏一个列。</P>
<P>　　好了，下面让大家跟我一起来创建一个DynamicDataGrid的C#的ASP.NET的工程，他有隐藏和显示DataGrid 的每一列的选项。</P>
<P>　　在我用VS.NET创建的web application里面,我在设计页面上拖放了一个Panel 控件。在这个panel控件上，我放置了一个DataGrid控件，一个DropDownList控件，两个Button控件用于改变DataGrid控件的属性。最后的设计界面看起来是下图这样的。</P>
<P><IMG height=388 alt="" src="http://www.blueidea.com/articleimg/2003/11/1295/1.jpg" width=510 border=0></P>
<P><BR>　　现在我们开始创建两个方法：FillDataGrid()和FillColumnsList()方法。FillDataGrid()用于增加一个列到DataGrid控件和用DataSet数据源来填充它。在这里的我是通过DB.GetDataSet()方法来获取DataSet的。大家可以参照附加的源代码文件(DB.CS)获取更多的细节。</P>
<P>　　下面的代码说明了CreateDataGrid()的实现。从代码中可以看出，我创建了三个列，用BoundColumn的DataField属性绑定到Dataset的ID,Name和Address字段。BoundColumn类继承自DataGridColumn类。</P>
<P>代码拷贝框<BR><TEXTAREA class=codeTextarea style="WIDTH: 596px; HEIGHT: 215px" rows=11 cols=65>private void CreateDataGrid()
{
// Set DataGrid properties
DataGrid1.AutoGenerateColumns = false;

// Get a DataSet object filled with data
DataSet ds = DB.GetDataSet();

// Create ID column &amp; add to DataGrid
BoundColumn col = new BoundColumn();
col.HeaderText="User ID"; 
col.DataField="ID";
DataGrid1.Columns.Add(col); 

// Create Name column &amp; add to DataGrid
col = new BoundColumn();
col.HeaderText="User Name"; 
col.DataField="Name";
DataGrid1.Columns.Add(col); 

// Create Address column &amp; add to DataGrid
col = new BoundColumn();
col.HeaderText="User Address"; 
col.DataField="Address";
DataGrid1.Columns.Add(col); 

// DataGrid data binding
DataGrid1.DataSource = ds.Tables[0];
DataGrid1.DataBind();
} 

　　//FillColumnsList()方法只是简单的从DataGrid中读取列名，并且将这些列(Columns)名填充到DropDownList控件的下拉列表中去。我们将使用DropDownList控件来选取隐藏或者显示的列。

private void FillColumnsList(DataGrid grid)
{
foreach (DataGridColumn col in grid.Columns)
{
ColumnsList.Items.Add(col.HeaderText);
}
}


　　//接着我们来添加HideDataGridColumn()方法来具体的通过index索引和bool值两个参数来显示或者隐藏一个列。在这里，我仅仅是简单的设置Columns列的Visible属性为true或者false.

private void HideDataGridColumn(int index, bool show)
{
DataGrid1.Columns[index].Visible = show; 
}


　　//最后的工作就是增加Show Column和Hide Column的单击事件处理。正如我们在代码中看到的一样，我只是简单的调用HideDataGridColumn()方法来显示或者隐藏列，当然要传入参数了。

private void HideColumnBtn_Click(object sender, System.EventArgs e)
{
HideDataGridColumn(ColumnsList.SelectedIndex, false);
this.DataBind();
}
private void ShowColumnBtn_Click(object sender, System.EventArgs e)
{
HideDataGridColumn(ColumnsList.SelectedIndex, true);
this.DataBind(); 
} 


　　
</TEXTAREA><BR>[Ctrl+A 全部选择 然后拷贝]</P>
<P>　　OK，所有的工作都结束了，我们看看运行的结果如何。你可以通过下拉列表来选择要隐藏或者显示的列，并且只需单击Show或者Hide按钮即可。</P>
<P align=center><IMG height=348 alt="" src="http://www.blueidea.com/articleimg/2003/11/1295/20033282152965464.jpg" width=449 border=0></P><img src ="http://www.cnitblog.com/piaoye12345/aggbug/3352.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/piaoye12345/" target="_blank">萌芽的叶子</a> 2005-10-17 14:05 <a href="http://www.cnitblog.com/piaoye12345/archive/2005/10/17/3352.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>