C# C DataGridView:如何更改单个单元格的值
我有两张表格。第一个表单包含datagridview,在第二个表单上,用户可以在第一个表单中更改datagridview中的单个值。问题是,如果用户更改一个值,其他单元格中的相同值也会更改 在以下日期前交货: A、B、C、D B B C D E 如果我尝试将A后的一个B更改为Z,它将更改所有B: A Z C D Z Z C D E 表格1:C# C DataGridView:如何更改单个单元格的值,c#,datagridview,cell,streamreader,streamwriter,C#,Datagridview,Cell,Streamreader,Streamwriter,我有两张表格。第一个表单包含datagridview,在第二个表单上,用户可以在第一个表单中更改datagridview中的单个值。问题是,如果用户更改一个值,其他单元格中的相同值也会更改 在以下日期前交货: A、B、C、D B B C D E 如果我尝试将A后的一个B更改为Z,它将更改所有B: A Z C D Z Z C D E 表格1: private void callForm2(object sender, EventArgs e) { Form2 f2 = new Form2(
private void callForm2(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.Text = this.dataGridView1.CurrentCell.Value.ToString();
}
表格2:
private void button1_Click(object sender, EventArgs e)
{
string edit = File.ReadAllText(@"C:\Master.txt");
edit = edit.Replace(Text, textBox1.Text); //first Text = old value, textBox1 = new value
File.WriteAllText(@"C:\Master.txt", edit);
string message = "Item is modified on the list!";
string caption = "Success";
MessageBoxButtons button = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Information;
MessageBox.Show(message, caption, button, icon);
this.Close();
//Changes Successfully, but if the cell value is same it changes all the same values
}
表单1从文本文件中获取数据
private void button1_Click(object sender, EventArgs e) //Add txt to program
{
if (File.Exists(@"C:\Master.txt")) //Look fot txt
{
using (StreamReader master = File.OpenText(@"C:\Master.txt")) //txt to table
{
int row = 0;
string s = String.Empty;
while ((s = master.ReadLine()) != null)
{
string[] columns = s.Split(',');
dataGridView1.Rows.Add();
for (int i = 0; i < columns.Length; i++)
{
dataGridView1[i, row].Value = columns[i];
}
row++;
}
}
}
else
{
string message = "Program was not able to find the text file.";
string caption = "Failed";
MessageBoxButtons button = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Warning;
MessageBox.Show(message,caption,button,icon);
}
}
通过执行edit=edit.ReplaceText,textBox1.Text;您正在替换所有出现的字符/字符串。。。
试试这样的
var regex = new Regex(Regex.Escape("A"));
var newText = regex.Replace(Text, textBox1.text, 1);
希望这有助于解决问题:
当实际目标是更改与DataGridView单元格对应的特定行上的一个特定逗号分隔值时,可以对文件的整个内容使用String.Replace方法
解决方案:
要仅修改所需的字符串记录,您必须知道它在DGV和文本文件中的哪一行和哪一列。您可以从this.dataGridView1.CurrentCell获取对应的RowIndex和ColumnIndex属性的行和列
您可以按照与文本相同的方式将它们提交给第二个表单,但不要忘记创建适当的属性:
f2.Text = this.dataGridView1.CurrentCell.Value.ToString();
f2.RowIndex = this.dataGridView1.CurrentCell.RowIndex;
f2.ColumnIndex = this.dataGridView1.CurrentCell.ColumnIndex;
public class Form2
{
public Int32 RowIndex {get; set;}
// ...
}
所以,现在你知道了变化应该发生在什么位置。虽然可以手动浏览文件并仅更改所需的值以简化操作,但我建议您使用稍微不同的方法:
不仅将数据加载到datagridview中,还将其存储在相应的交错数组中:
public class MyForm
{
// To store your currently loaded values
public List<String[]> Values{get; set;}
// Load button
...
using (StreamReader master = File.OpenText(@"C:\Master.txt")) //txt to table
{
this.values = new List<String[]>();
int row = 0;
string s = String.Empty;
while ((s = master.ReadLine()) != null)
{
string[] columns = s.Split(',');
// Save the line
values.Add(columns);
dataGridView1.Rows.Add();
for (int i = 0; i < columns.Length; i++)
{
dataGridView1[i, row].Value = columns[i];
}
row++;
}
}
然后在form2按钮1上单击:
private void button1_Click(object sender, EventArgs e)
{
this.Values[this.RowIndex][this.ColumnIndex] = textBox1.Text;
string edit = String.Join(
Environment.NewLine,
this.Values.
Select(array =>
String.Join(",", array)).
ToArray());
File.WriteAllText(@"C:\Master.txt", edit);
// ...
}
这不是最有效或最好的解决方案,但它应该有效
注意:如果您想提高对文件IO的理解,可以尝试在不存储和完全重写其内容的情况下更改文件中的值。DataGridView中的数据是什么?如何获取并添加到DGV?如何以及何时更新?DGV从文本文件获取数据。在表格2中,有一个文本框。单击按钮1时,它会更改所选单元格并将其写入文本文件问题的根源显而易见-替换替换DGV中反映的每个旧子字符串。现在的问题是如何从文件中读取值并将其放入DGV?我使用Streamreader读取表1中的值。然后,在表单2中,我只是使用WriteAllText将更改后的值写入文本文件。有没有办法替换一个单元格而不是所有的子字符串?如何解析文件并将其内容放入DataGridView?或者文件和DGV之间是否存在更复杂的关系?这将删除DGV中的所有值感谢您的回答。我假设f2.ColumnIndex=this.dataGridView1.CurrentCell.RowIndex;应为f2.ColumnIndex=this.dataGridView1.CurrentCell.ColumnIndex;另外,在表单2中添加公共列表值{get;set;}。但是,当我单击按钮时,它会删除整行,并且只显示System.Char[]@user2349228修复了一些小问题,应该可以正常工作。很抱歉没有反应,只是错过了评论通知。非常感谢!问题解决了!!
private void button1_Click(object sender, EventArgs e)
{
this.Values[this.RowIndex][this.ColumnIndex] = textBox1.Text;
string edit = String.Join(
Environment.NewLine,
this.Values.
Select(array =>
String.Join(",", array)).
ToArray());
File.WriteAllText(@"C:\Master.txt", edit);
// ...
}