将行从GridView绑定到DataTable并在另一个GridView中显示它 [英] Bind Rows From GridView to DataTable and Display It In Another GridView

查看:68
本文介绍了将行从GridView绑定到DataTable并在另一个GridView中显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从gridview中的列中找到一个值。如果该列中有行具有此值,我将这些行添加到DataTable中。然后我将它绑定到另一个gridview以显示这些行。但是,当我尝试这样做时,INT中的列不能在INT中显示出来,但只能在TEXT中显示出来。此外,只有1行被绑定到数据表并显示在另一个gridview中,如果许多行在该列中具有特定值,而不仅仅是1行,我想要很多行。



这是我使用的代码:

I am going to find a value from a column in a gridview. If there are rows that have this value in that column, I will add those rows to a DataTable. Afterwards I will bind it to another gridview to display those rows out. However, when I tried to do this, columns that are in INT cannot be displayed out in INT but can only be displayed out in TEXT. Also, only 1 row is being binded to the datatable and displayed out in another gridview, I want many rows if many rows have the particular value in that column, not just 1 row.

This is the code I used:

public void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
 DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add(new DataColumn("DATE"));
            dt.Columns.Add(new DataColumn("CODE"));
            dt.Columns.Add(new DataColumn("PERSON_NAME"));
            dt.Columns.Add(new DataColumn("STATUS"));
            dt.Columns.Add(new DataColumn("HOBBIES"));
            dt.Columns.Add(new DataColumn("SCORE"));
            dt.Columns.Add(new DataColumn("ITEM"));
            dt.Columns.Add(new DataColumn("QUANTITY"));
            dt.Columns.Add(new DataColumn("TYPE"));
            dt.Columns.Add(new DataColumn("RATING"));
            dt.Columns.Add(new DataColumn("PRICE"));
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "STATUS")) == "Regular")
                    {
                        dr = dt.NewRow();
                        dr["DATE"] = e.Row.Cells[0].Text;
                        dr["CODE"] = e.Row.Cells[1].Text;
                        dr["PERSON_NAME"] = e.Row.Cells[2].Text;
                        dr["STATUS"] = e.Row.Cells[3].Text;
                        dr["HOBBIES"] = e.Row.Cells[4].Text;
                        dr["SCORE"] = e.Row.Cells[5].Text;
                        dr["ITEM"] = e.Row.Cells[6].Text;
                        dr["QUANTITY"] = e.Row.Cells[7].Text;
                        dr["TYPE"] = e.Row.Cells[8].Text;
                        dr["RATING"] = e.Row.Cells[9].Text;
                        dr["PRICE"] = e.Row.Cells[10].Text;
                        dt.Rows.Add(dr);
                        GridView2.DataSource = dt;
                        GridView2.DataBind();
                    }



有人可以帮我吗?非常感谢!!


Can someone please help me on this? Thanks a lot!!

推荐答案

我认为正确的版本将是



I think correct version would be

new DataColumn("STATUS", GetType(int)); 





或类似的东西(从记忆中写下来。



or something very similar (writing from memory.


这篇关于将行从GridView绑定到DataTable并在另一个GridView中显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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