将gridview转换为datatable [英] convert gridview to datatable

查看:66
本文介绍了将gridview转换为datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

heyyy ....



我有一个带数据的网格..我想将这个网格数据传输到数据表上按钮点击..



我试过但我没有得到网格中的值..这里我的代码..









heyyy....

I have a grid with data..I want transfer this gridrows data to a datatable on a button click..

I tried but I don''t get the values in grid.. here my code..




foreach (GridViewRow row in grvBaseFareGraph.Rows)
             {
                 DataRow dr;
                 dr = dtn.NewRow();

                 for (int i = 1; i < row.Cells.Count; i++)
                 {
                     dr[i] = row.Cells[3].Text;
                 }
                 dtn.Rows.Add(dr);
             }







plz help ...




plz help...

推荐答案

尝试这个..

try this..
DataTable GetDataTable(GridView dtg)
        {
            DataTable dt = new DataTable();
 
            // add the columns to the datatable            
            if (dtg.HeaderRow != null)
            {
 
                for (int i = 0; i < dtg.HeaderRow.Cells.Count; i++)
                {
                    dt.Columns.Add(dtg.HeaderRow.Cells[i].Text);
                }
            }
 
            //  add each of the data rows to the table
            foreach (GridViewRow row in dtg.Rows)
            {
                DataRow dr;
                dr = dt.NewRow();
 
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    dr[i] = row.Cells[i].Text.Replace(" ", "");
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }


嘿尝试下面的代码..

hey try below code..
DataTable yourDataTable = yourGridView.DataSource as DataTable


这篇关于将gridview转换为datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆