DatagridView 在连接归档时显示错误
DatagridView displaying error when connected with filing
在 filing
惊人地保存数据和检索 DataGridView
上的记录时出现问题这是我正在尝试的代码,但它也显示列的 Last Value
和 1st value
像这样的下一列是文件中的信息,文件中的数据如下:
3519,laiba,99
3519,maheen,89
这是我的代码:
private void btnsave_Click(object sender, EventArgs e)
{
FileStream f = new FileStream("D://abc.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(f);
sw.Write(txtregno.Text+","+txtname.Text+","+txtmarks.Text+";");
MessageBox.Show("Saved Successfully");
sw.Close();
}
private void btnshow_Click(object sender, EventArgs e)
{
FileStream f = new FileStream("D://abc.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(f);
string data = sr.ReadToEnd();
string[] lineWiseRecord = data.Split(';');
foreach (string item in lineWiseRecord)
{
string[] colWiseRecord = item.Split(',');
dataGridView1.Rows.Add(colWiseRecord[0], colWiseRecord[1], colWiseRecord[2] );
}
f.Close();
sr.Close();
}
您的代码将抛出错误,不能 运行。为datagridview指定列数,然后通过foreach循环将数据加载到datagirdview中,这样就可以看到txt文档中的所有数据了。这是我的代码,你可以试试下面的代码来替换它。
private void btnsave_Click(object sender, EventArgs e)
{
FileStream f = new FileStream("D://abc.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(f);
sw.Write(txtregno.Text + "," + txtname.Text + "," + txtmarks.Text + ";");
MessageBox.Show("Saved Successfully");
sw.Close();
}
private void btnshow_Click_1(object sender, EventArgs e)
{
FileStream f = new FileStream("D://abc.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(f);
string data = sr.ReadToEnd();
string[] lineWiseRecord = data.Split(';');
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "regno";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[2].Name = "Mark%";
foreach (string item in lineWiseRecord)
{
string[] colWiseRecord = item.Split(',');
dataGridView1.Rows.Add(colWiseRecord);
}
f.Close();
sr.Close();
}
结果:
filing
惊人地保存数据和检索 DataGridView
上的记录时出现问题这是我正在尝试的代码,但它也显示列的 Last Value
和 1st value
像这样的下一列是文件中的信息,文件中的数据如下:
3519,laiba,99
3519,maheen,89
这是我的代码:
private void btnsave_Click(object sender, EventArgs e)
{
FileStream f = new FileStream("D://abc.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(f);
sw.Write(txtregno.Text+","+txtname.Text+","+txtmarks.Text+";");
MessageBox.Show("Saved Successfully");
sw.Close();
}
private void btnshow_Click(object sender, EventArgs e)
{
FileStream f = new FileStream("D://abc.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(f);
string data = sr.ReadToEnd();
string[] lineWiseRecord = data.Split(';');
foreach (string item in lineWiseRecord)
{
string[] colWiseRecord = item.Split(',');
dataGridView1.Rows.Add(colWiseRecord[0], colWiseRecord[1], colWiseRecord[2] );
}
f.Close();
sr.Close();
}
您的代码将抛出错误,不能 运行。为datagridview指定列数,然后通过foreach循环将数据加载到datagirdview中,这样就可以看到txt文档中的所有数据了。这是我的代码,你可以试试下面的代码来替换它。
private void btnsave_Click(object sender, EventArgs e)
{
FileStream f = new FileStream("D://abc.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(f);
sw.Write(txtregno.Text + "," + txtname.Text + "," + txtmarks.Text + ";");
MessageBox.Show("Saved Successfully");
sw.Close();
}
private void btnshow_Click_1(object sender, EventArgs e)
{
FileStream f = new FileStream("D://abc.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(f);
string data = sr.ReadToEnd();
string[] lineWiseRecord = data.Split(';');
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "regno";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[2].Name = "Mark%";
foreach (string item in lineWiseRecord)
{
string[] colWiseRecord = item.Split(',');
dataGridView1.Rows.Add(colWiseRecord);
}
f.Close();
sr.Close();
}
结果: