为什么在CS里可以使用
public DataTable GetDataTable(DataGrid dataGrid)
{
DataTable dt = (DataTable)dataGrid.DataSource;
}
可以得到数据,而在BS里DataTable却是空的。
请教高手能否将DataGrid或Gridview 里绑定的数据转换成DataTable取出来?怎么实现?
public DataTable GetDataTable(DataGrid dataGrid)
{
DataTable dt = (DataTable)dataGrid.DataSource;
}
可以得到数据,而在BS里DataTable却是空的。
请教高手能否将DataGrid或Gridview 里绑定的数据转换成DataTable取出来?怎么实现?
解决方案 »
- RowEditing()事件和RowDeleting()事件没用了!
- 为什么上传压缩文件会无法显示该网页
- 如何把asp.net项目发布到IIS中
- 神奇的图片是怎么做到的呢?
- web服务使用问题
- 路过的大侠们:免费的asp.net版的eWebEditor??谁有啊??
- 关于ShowModalDialog,想在关闭子窗口后刷新父窗口怎么作,
- windows下和web服务器通讯获得数据库中数据的程序编制思路?
- 系统还原后,原来的网页不能调试了?
- 一个菜鸟问题!在线等待!
- 调查下,现在多少人用VS。NET2008这款开发工具
- Asp.net Url 重写成功(使用Intelligencia.UrlRewriter),但Google不收录重定向后的页面,请大家帮忙
DataView dv = (DataView)GridView1.DataSource;
dv.ToTable();
除了这个方法还有别的方法吗?public static string GetCellText(TableCell cell)
{
string text = cell.Text;
if (!string.IsNullOrEmpty(text))
{
return text;
}
foreach (Control control in cell.Controls)
{
if (control != null && control is IButtonControl)
{
IButtonControl btn = control as IButtonControl;
text = btn.Text.Replace("\r\n", "").Trim();
break;
}
if (control != null && control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
if (lc != null)
{
continue;
}
ITextControl l = control as ITextControl; text = l.Text.Replace("\r\n", "").Trim();
break;
}
}
return text;
}
/**/
/// <summary>
/// 从GridView的数据生成DataTable
/// </summary>
/// <param name="gv">GridView对象</param>
public static DataTable GridView2DataTable(GridView gv)
{
DataTable table = new DataTable();
int rowIndex = 0;
List<string> cols = new List<string>();
if (!gv.ShowHeader && gv.Columns.Count == 0)
{
return table;
}
GridViewRow headerRow = gv.HeaderRow;
int columnCount = headerRow.Cells.Count;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(headerRow.Cells[i]);
cols.Add(text);
}
foreach (GridViewRow r in gv.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
DataRow row = table.NewRow();
int j = 0;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(r.Cells[i]);
if (!String.IsNullOrEmpty(text))
{
if (rowIndex == 0)
{
string columnName = cols[i];
if (String.IsNullOrEmpty(columnName))
{
continue;
}
if (table.Columns.Contains(columnName))
{
continue;
}
DataColumn dc = table.Columns.Add();
dc.ColumnName = columnName;
dc.DataType = typeof(string);
}
row[j] = text;
j++;
}
}
rowIndex++;
table.Rows.Add(row);
}
}
return table;
}
{
return table;
}服务器端DataSet或者DateTbale之流绑定给GridView之后,GridView只会按照字段的顺序显示所有列,此时无论GridView的AutoGenerateColumns属性启用还是不启用,Columns对象的Count将为0,因为GridView还是会自动生成的列,而自动生成列是不会放入到Columns集合中去的.这段代码应该没有问题
foreach (GridViewRow r in gv.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
DataRow row = table.NewRow();
int j = 0;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(r.Cells[i]);
if (!String.IsNullOrEmpty(text))
{
if (rowIndex == 0)
{
string columnName = cols[i];
if (String.IsNullOrEmpty(columnName))
{
continue;
}
if (table.Columns.Contains(columnName))
{
continue;
}
DataColumn dc = table.Columns.Add();
dc.ColumnName = columnName;
dc.DataType = typeof(string);
}
row[j] = text;
j++;
}
}
rowIndex++;
table.Rows.Add(row);
}
}
楼主参见 http://blog.csdn.net/linqingfeng/archive/2007/10/14/1824259.aspx
GridViewRow headerRow = gv.HeaderRow;
int columnCount = headerRow.Cells.Count;
{
return table;
}
??????
!gv.ShowHeader && gv.Columns.Count == 0){
return table;
}
??????
和dataview是不同的吧
用循环取值不就好了~~