为什么在CS里可以使用
public DataTable GetDataTable(DataGrid dataGrid)
{
    DataTable dt = (DataTable)dataGrid.DataSource;
}
可以得到数据,而在BS里DataTable却是空的。
请教高手能否将DataGrid或Gridview 里绑定的数据转换成DataTable取出来?怎么实现?

解决方案 »

  1.   

    试试        
    DataView dv = (DataView)GridView1.DataSource;
            dv.ToTable();
      

  2.   

    两个循环去取值再放到DATATABLE中吧....
      

  3.   

    这最简单不过了GridView 是绑定的哪个数据源就用哪个例如。你的 gridview.datasource= ds那你就取 ds 就是了
      

  4.   

    可我要传递的是GridView 啊!
    除了这个方法还有别的方法吗?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;
        } 
      

  5.   

    if (!gv.ShowHeader && gv.Columns.Count == 0)
            {
                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
      

  6.   

    楼主的这段代码 是否可以得到 列的数目呢?
    GridViewRow headerRow = gv.HeaderRow;
            int columnCount = headerRow.Cells.Count;
      

  7.   

    因为,一般,在你点击button后,GridView的datasource = NULL,数据是由ViewState显示的。不知道有没有明白呢?在B/S要获取数据,还得执行一遍SQL而C/S,因为数据在PC的内存当中,Datasource不会自己没有的。
      

  8.   

      if (!gv.ShowHeader && gv.Columns.Count == 0)
            {
                return table;
            }
    ??????
      

  9.   

     if (!gv.ShowHeader && gv.Columns.Count == 0) 
            { 
                return table; 
            } 
    ??????
      

  10.   

    gridview
    和dataview是不同的吧
    用循环取值不就好了~~