如何使用DataView进行数据排序和检索

显示结果:
CustomerID CompanyName Country
WHITC White Clover Markets USA
TRAIH Trail's Head Gourmet Provisioners USA
THECR The Cracker Box USA
THEBI The Big Cheese USA
SPLIR Split Rail Beer & Ale USA
SAVEA Save-a-lot Markets USA
RATTC Rattlesnake Canyon Grocery USA
OLDWO Old World Delicatessen USA
LONEP Lonesome Pine Restaurant USA
LETSS Let's Stop N Shop USA
LAZYK Lazy K Kountry Store USA
HUNGC Hungry Coyote Import Store USA
GREAL Great Lakes Food Market USA

源代码:

void  Page_Load( object  sender, System.EventArgs e)
            
{
                
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
                
string Sql = "SELECT CustomerID, CompanyName, Country FROM Customers";

                SqlConnection thisConnection 
= new SqlConnection(ConnectionString);
                SqlDataAdapter adapter 
= new SqlDataAdapter(Sql, thisConnection);

                
// 创建DataTable对象
                DataTable table = new DataTable();

                
// 填充数据到DataTable
                adapter.Fill(table);

                
// 创建DataView,并在构造函数中得到排序和检索条件等参数
                DataView dataView = new DataView(table, "Country='USA'""CompanyName DESC", DataViewRowState.CurrentRows);

                
// 数据绑定
                myDataGrid.DataSource = dataView;
                myDataGrid.DataBind();
            }
        

你可能感兴趣的:(view)