我从后台取出一个DataTable,因为前台要有筛选功能,所以要用DataView,具体代码如下
private void RefreshGrid()
{
DPSWeb.RoleWebService.RoleWebService aa = new DPSWeb.RoleWebService.RoleWebService();
DataSet ds = aa.GetRoleDetail("admin","admin","admin",Convert.ToInt32(Session["nRoleID"]));//调后代的WebService
if(ds ==null)return;
if(ds.Tables.Count<1)return; if(ds.Tables["t_roles"].Rows.Count>0)
TextBox1.Text = ds.Tables["t_roles"].Rows[0]["rolename"].ToString();
if(ds.Tables["t_tables"].Rows.Count>0)
{

DropDownList1.DataSource = ds.Tables["t_tables"];
DropDownList1.DataTextField = "tableheadername";
DropDownList1.DataValueField = "tableid";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0,new ListItem("请选择表----"));
}
DataGrid1.DataSource = ds.Tables["t_privileges"].DefaultView;
DataView dv = ds.Tables["t_privileges"].DefaultView;
Cache["dv"] = dv; 
ds.Tables["t_tables"].PrimaryKey = new DataColumn[]{ds.Tables["t_tables"].Columns["tableid"]};//建立主键
Cache["m_bLoad"] = false;
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(Convert.ToBoolean(Cache["m_bLoad"])==false)
{
DataView dv =(DataView)Cache["dv"];
if(DropDownList1.SelectedItem.Value !=null && DropDownList1.SelectedIndex !=0)
{
dv.RowFilter = "tableid =" + DropDownList1.SelectedItem.Value;//筛选记录绑定DataGrid
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}

}
}
筛选是按照DropDownList里的值来选择的,再将索筛选的记录放到dataGrid上。
现在的问题是,我要更新DataTable了,但是DataView里行和DataTable里的行不是对应的(因为经过筛选),所以我在作循环的时候就无确定i
int i=0;
foreach(DataGridItem item in DataGrid1.Items)//取出datagrid的所有items
{
cb1 = (CheckBox)item.FindControl(cb1);

if(cb1.Checked)
ds.Tables["t_privileges"].Rows[i]["canview"] =1;//i是无法确定的
else
ds.Tables["t_privileges"].Rows[i]["canview"] =0;
I++}
因为DataGrid上的列和DataTable里的列不是一一对应的(经过了DataView的筛选)
请高手指教,我已经弄了一下午了!唉