簡體   English   中英

Datagridview.datasource = null

[英]Datagridview.datasource=null

我想過濾datagridview中的數據。 我想用以下代碼做到這一點:

DataView view = new DataView();
DataTable dt = new DataTable();
dt = Tbl_events.DataSource as DataTable;
view = dt.DefaultView; 
view.RowFilter = "Type='1301'";
Tbl_events.DataSource = view;

這給了我一個空的datagridview。 調試代碼時,我在此行上看​​到:

dt = Tbl_events.DataSource as DataTable;
Tbl_events.DataSource = null;

但是datagridview Tbl_events中有數據。

我究竟做錯了什么?

嘗試以下代碼:

您正在使用添加行。 首先轉換為數據表,然后使用過濾並將數據表添加到datagridview

     foreach (DataGridViewColumn c in dataGridView1.Columns)
            {
                dt.Columns.Add(new DataColumn(c.HeaderText.ToString()));
            }


        for (int row = 0; row < dataGridView1.Rows.Count; row++)
        {
            // create a new dt row to match up with the rows in the DGV
            DataRow dr = dt.NewRow();
            int column = 0;
            foreach (DataGridViewColumn c in dataGridView1.Columns)
            {
                //Sure you can see what this is doing.
                dr[c.HeaderText.ToString()] = dataGridView1.Rows[row].
                   Cells[column].Value;
                column++;
            }

            //now i add the row to the data table.
            dt.Rows.Add(dr);
        }


        dataGridView1.Rows.Clear();
 dt = dt.Select("Type='1301'").CopyToDataTable();
            dataGridView1.DataSource = dt;

編輯

 DataView view = new DataView();
            view = dt.DefaultView;
            view.RowFilter = "Type='1301'"
 dataGridView1.DataSource = view;

view.RowFilter =“ Type ='1301'”;

view.RowStateFilter = DataViewRowState.CurrentRows;

//以下調用了另一種方法來顯示

//數據視圖內容。

Tbl_events.DataSource = DataView1;

Tbl_events.DataBind();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM