如何保持Orignal DataTable源不被改变 [英] How to Keep Orignal DataTable Source from Changing
问题描述
我有3个列的表 两个字符串和一个布尔值
我有2个按钮恢复原始并保存新
我需要的是如果我在保存新的更改前点击恢复按钮所有原始数据返回
所以我 创建全局数据 Origenal 然后从活动目录中检索
某些信息到此DataTAble
DataTable Origenal;
$
public void CreateMyTable()
{
Origenal = new DataTable();
Origenal .Columns.Add(" GroupID",typeof(string));
Origenal .Columns.Add(" Name",typeof(string));
var column = new DataColumn(" flag",typeof(bool));
column.DefaultValue = false;
Origenal .Columns.Add(column);}
然后检索 某些 从活动目录到此
的信息 Origenal Table
        
UserGrupe GetUserGrupe = new UserGrupe();
Origenal = GetUserGrupe .FindOu();
当我绑定Datable with DataGridView
dataGridView1.DataSource = Origenal
Origenal DataTable结果
GroupID 名称         flag
A_1 AB       是的
A_2 CC         错误
A_3 DD         错误
在 GridView 如果我更改数据类似
示例 Origenal DataTable
GroupID 姓名
flag
A_1   ;  AB             
错误
A_2 CC             
True
A_3 DD             
错误
< span style ="">
然后 点击恢复按钮恢复原始数据
dataGridView1.DataSource = Origenal;
结果是
的GroupID&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; 姓名
flag
A_1   ;&NBSP; AB&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
True
A_2 CC&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
True
A_3 DD&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
错误
A_1标志返回原始值
True
但 A_2 标记保留 原始值为假时为真
并保持原始表数据我尝试克里特新表
DataTable copyTable = new DataTable();
copyTable = Origenal;
dataGridView1.DataSource = copyTable;
但仍然返回相同的结果
GroupID 姓名
flag
A_1   ;&NBSP; AB&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
True
A_2 CC&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
True
A_3 DD&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
错误
< span style ="">
我也试过
dataGridView1.DataSource = Origenal.Copy() ;
仍然是相同的结果
然后我尝试其他解决方案
DataTable copyTable = new DataTable();
copyTable = Origenal.copy();dataGridView1.DataSource = copyTable;
结果是我想要的原始数据
< p style =""> GroupID 姓名
flag
A_1   ;&NBSP; AB&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;真实
A_2 CC&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
错误
A_3 DD&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
错误
但 我不明白这种情况发生了,如果有另一种解决方案
我还有另一个问题如何禁用第一拖dataGridView1中的列来自编辑
不是DataTable
您好
感谢您发布此处。
对于您的问题,您希望保持原始DataTable源不会更改和禁用来自编辑的datagridView1中的前两列。
您可以尝试以下代码。
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
DataTable Origenal = new DataTable();
public static DataTable CreateMyTable()
{
DataTable table = new DataTable();
table.Columns.Add(" GroupID",typeof(string));
table.Columns.Add(" Name",typeof(string));
var column = new DataColumn(" flag",typeof(bool));
column.DefaultValue = false;
table.Columns.Add(column);
返回表;
}
private void Button1_Click(object sender,EventArgs e)
{
UserGrupe GetUserGrupe = new UserGrupe();
Origenal = GetUserGrupe.FindOu();
this.dataGridView1.DataSource = Origenal;
dataGridView1.Columns [0] .ReadOnly = true; //禁用第一列
dataGridView1.Columns [1] .ReadOnly = true; //禁用第二列
}
公共类UserGrupe
{
public DataTable FindOu()
{
DataTable newtable = CreateMyTable();
newtable.Rows.Add(" A_1"," AB",true);
newtable.Rows.Add(" A_2"," CC",false);
newtable.Rows.Add(" A_3"," DD",false);
返回newtable;
}
}
}结果:
最诚挚的问候,
杰克
I have Table that have 3 Columns tow string and one Boolean
and I have 2 button Restore Original and save New
what I need is if I click restore button before save new change all the original Data to return
so I create Global Datable Origenal then Retrieve certain Information From active Directory into this DataTAble
DataTable Origenal ;
public void CreateMyTable()
{
Origenal = new DataTable(); Origenal .Columns.Add("GroupID", typeof(string)); Origenal .Columns.Add("Name", typeof(string)); var column = new DataColumn("flag", typeof(bool)); column.DefaultValue = false; Origenal .Columns.Add(column);
}
then Retrieve certain Information From active Directory into this Origenal Table
UserGrupe GetUserGrupe = new UserGrupe (); Origenal = GetUserGrupe .FindOu();
when I bind the Datable with DataGridView
dataGridView1.DataSource =Origenal
Origenal DataTable Result
GroupID Name flag
A_1 AB True
A_2 CC False
A_3 DD False
In the GridView if I change Data Like
Example Origenal DataTable
GroupID Name flag
A_1 AB False
A_2 CC True
A_3 DD False
then click restore button to restore original data
dataGridView1.DataSource = Origenal;
The Result is
GroupID Name flag
A_1 AB True
A_2 CC True
A_3 DD False
the A_1 flag return to original Value True
but A_2 flag remain True when the original Value is False
and to keep the Original Table Data I try to Crete new Table
DataTable copyTable = new DataTable(); copyTable = Origenal; dataGridView1.DataSource = copyTable ;
but still return the same result
GroupID Name flag
A_1 AB True
A_2 CC True
A_3 DD False
I also tried
dataGridView1.DataSource = Origenal.Copy();
still the Same result
then I try another solution
DataTable copyTable = new DataTable();
copyTable = Origenal.copy();dataGridView1.DataSource = copyTable ;
the result was the original Data I wanted
GroupID Name flag
A_1 AB True
A_2 CC False
A_3 DD False
but I did not understand way this happened and if there is another solution
also I have another Question how to Disable first tow column in the dataGridView1 from edit
not the DataTable
Hi
Thank you for posting here.
For your question, you want to keep original DataTable source from changing and disable first two columns in the datagridView1 from edit.
You could try the following code.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable Origenal=new DataTable(); public static DataTable CreateMyTable() { DataTable table = new DataTable(); table.Columns.Add("GroupID", typeof(string)); table.Columns.Add("Name", typeof(string)); var column = new DataColumn("flag", typeof(bool)); column.DefaultValue = false; table.Columns.Add(column); return table; } private void Button1_Click(object sender, EventArgs e) { UserGrupe GetUserGrupe = new UserGrupe(); Origenal = GetUserGrupe.FindOu(); this.dataGridView1.DataSource = Origenal; dataGridView1.Columns[0].ReadOnly = true;//disable first column dataGridView1.Columns[1].ReadOnly = true;//disable second column } public class UserGrupe { public DataTable FindOu() { DataTable newtable = CreateMyTable(); newtable.Rows.Add("A_1", "AB", true); newtable.Rows.Add("A_2", "CC", false); newtable.Rows.Add("A_3", "DD", false); return newtable; } } }Result:
Best Regards,
Jack
这篇关于如何保持Orignal DataTable源不被改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!