private void tvResources_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
///根据选定状态为条件修改权限
///
if(flag)
{
DataRowView rowCategory = this.BindingContext[this.dsCategory, this.dsCategory.Tables[0].TableName].Current as DataRowView;
string categoryid = rowCategory["C_CATEGORYID"].ToString();
string resourceid = ((Subject)e.Node.Tag).data["C_RESOURCEID"].ToString();
//string resourceid =((Subject)tvResources.SelectedNode.Tag).data["C_RESOURCEID"].ToString();
DataSet dsGrant1=DataBaseAgent.GetDataFromDB("select c_categoryid,c_subjectid,c_resourceid from TR_GRANT");//(DataBaseAgent.ORACLECONNECTSTRING,"select * from tr_grant");
if(e.Node.Checked)
{
//添加指定权限
DataRow rowGrant1= dsGrant1.Tables[0].NewRow();
//rowGrant.BeginEdit();
rowGrant1["c_categoryid"] = categoryid;
rowGrant1["c_subjectid"] = subjectno;
rowGrant1["c_resourceid"] = resourceid;
//MessageBox.Show(rowGrant1[0]+" "+rowGrant1[1]+" "+rowGrant1[2]);
dsGrant1.Tables[0].Rows.Add(rowGrant1);
}
else
{
//删除指定权限
DataRow rowGrant1= dsGrant1.Tables[0].NewRow();
//rowGrant.BeginEdit();
rowGrant1[0] = categoryid;
rowGrant1[1] = subjectno;
rowGrant1[2] = resourceid;
//MessageBox.Show(rowGrant1[0]+" "+rowGrant1[1]+" "+rowGrant1[2]);
dsGrant1.Tables[0].Rows.Remove(rowGrant1);
}
dsGrantn.Merge(dsGrant1); }
}
public class Subject
{
public DataRow data = null;
public Subject(DataRow row)
{
this.data = row;
}
public static TreeNode newSubjectNode(DataRow row)
{
Subject subject = new Subject(row);
TreeNode node = new TreeNode(subject.data[1].ToString());
node.Tag = subject;
return node;
}
}
{
///根据选定状态为条件修改权限
///
if(flag)
{
DataRowView rowCategory = this.BindingContext[this.dsCategory, this.dsCategory.Tables[0].TableName].Current as DataRowView;
string categoryid = rowCategory["C_CATEGORYID"].ToString();
string resourceid = ((Subject)e.Node.Tag).data["C_RESOURCEID"].ToString();
//string resourceid =((Subject)tvResources.SelectedNode.Tag).data["C_RESOURCEID"].ToString();
DataSet dsGrant1=DataBaseAgent.GetDataFromDB("select c_categoryid,c_subjectid,c_resourceid from TR_GRANT");//(DataBaseAgent.ORACLECONNECTSTRING,"select * from tr_grant");
if(e.Node.Checked)
{
//添加指定权限
DataRow rowGrant1= dsGrant1.Tables[0].NewRow();
//rowGrant.BeginEdit();
rowGrant1["c_categoryid"] = categoryid;
rowGrant1["c_subjectid"] = subjectno;
rowGrant1["c_resourceid"] = resourceid;
//MessageBox.Show(rowGrant1[0]+" "+rowGrant1[1]+" "+rowGrant1[2]);
dsGrant1.Tables[0].Rows.Add(rowGrant1);
}
else
{
//删除指定权限
DataRow rowGrant1= dsGrant1.Tables[0].NewRow();
//rowGrant.BeginEdit();
rowGrant1[0] = categoryid;
rowGrant1[1] = subjectno;
rowGrant1[2] = resourceid;
//MessageBox.Show(rowGrant1[0]+" "+rowGrant1[1]+" "+rowGrant1[2]);
dsGrant1.Tables[0].Rows.Remove(rowGrant1);
}
dsGrantn.Merge(dsGrant1); }
}
public class Subject
{
public DataRow data = null;
public Subject(DataRow row)
{
this.data = row;
}
public static TreeNode newSubjectNode(DataRow row)
{
Subject subject = new Subject(row);
TreeNode node = new TreeNode(subject.data[1].ToString());
node.Tag = subject;
return node;
}
}
以下是保存的代码:
private void button1_Click(object sender, System.EventArgs e)
{
//SaveGrant();
DataBaseAgent.UpdateDS(DataBaseAgent.ORACLECONNECTSTRING,this.dsGrantn,"TR_GRANT");
MessageBox.Show("数据保存成功!");
}
DataRow rowGrant1= dsGrant1.Tables[0].NewRow();只是创建这个DataTable结构的一个行,这个行和这个DataTable没有从属关系,所以你Remove的时候就出错插入好像看不出什么毛病,是不是主键之类的问题
row = FindRow(...);然后调用:
row.Delete();再保存就可以了!
rowGrant1[0].Delete();
rowGrant1[0].AcceptChanges();
///我这样改可以吗??