例如修改红色的部分进行过滤SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROm Categories",myConnection);DataSet ds = new DataSet();ad.Fill(ds,"Categories");DataGrid1.DataSource = ds;DataGrid1.DataBind();
这样没有得到新的dt啊,原来的dt不能变
如果只是要显示,那么可以用DataView得到原DataTable的视图。
再创建一个dt2,如: DataTable dt2 = dt; 对dt2进行操作!!
使用DataTable.Copy()生成新的DataTable,然后进行处理另外一个就是我说的,创建该表的视图:private void BindDataGrid() { DataTable table = new DataTable(); // Insert code to populate a DataTable with data. // Bind grid to DataTable. dataGrid1.DataSource = table; }private void ChangeRowFilter() { DataTable gridTable = (DataTable) dataGrid1.DataSource; // Set the RowFilter to display a company names that // begin with A through I.. gridTable.DefaultView.RowFilter = "CompanyName < 'I'"; }
粘个例子,哈哈哈private void GetRowsByFilter() { DataTable table = DataSet1.Tables["Orders"]; // Presuming the DataTable has a column named Date. string expression = "Date > '1/1/00'"; // Sort descending by column named CompanyName. string sortOrder = "CompanyName DESC"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = table.Select(expression, sortOrder); // Print column 0 of each returned row. for(int i = 0; i < foundRows.Length; i ++) { Console.WriteLine(foundRows[i][0]); } }
dt.Columns.Remove( "列名称 ");
dt.Columns.RemoveAt(列索引);
DataTable dt2 = dt;
对dt2进行操作!!
{
DataTable table = new DataTable(); // Insert code to populate a DataTable with data. // Bind grid to DataTable.
dataGrid1.DataSource = table;
}private void ChangeRowFilter()
{
DataTable gridTable = (DataTable) dataGrid1.DataSource; // Set the RowFilter to display a company names that
// begin with A through I..
gridTable.DefaultView.RowFilter = "CompanyName < 'I'";
}
根据要显示的哪些字段就添加上去这是其中的第一种
2 可以用绑定CheckBox 隐藏自己不想看的字段
再创建一个dt2,如:
DataTable dt2=dt.Copy();
对dt2进行操作!!
{
DataTable table = DataSet1.Tables["Orders"]; // Presuming the DataTable has a column named Date.
string expression = "Date > '1/1/00'"; // Sort descending by column named CompanyName.
string sortOrder = "CompanyName DESC";
DataRow[] foundRows; // Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression, sortOrder); // Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}