2
我將文本讀取到GridView,然後將此文本從GridView移動到文本框進行編輯。我如何從選擇行中替換已更改的數據並保存到我的文本文件中?當我從GridView移動到文本框時,如何更新文本文件
這是我寫文件
private void button1_Click(object sender, EventArgs e)
{
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"WriteLines2.txt", true))
{
file.WriteLine(this.id.Text + "#" + this.judul.Text + "#" + this.isi.Text);
}
}
這是我的GridView
System.IO.StreamReader file = new System.IO.StreamReader("WriteLines2.txt");
string[] columnnames = file.ReadLine().Split('#');
DataTable dt = new DataTable();
foreach (string c in columnnames)
{
dt.Columns.Add(c);
}
string newline;
while ((newline = file.ReadLine()) != null)
{
DataRow dr = dt.NewRow();
string[] values = newline.Split('#');
for (int i = 0; i < values.Length; i++)
{
dr[i] = values[i];
}
dt.Rows.Add(dr);
}
file.Close();
GridView.DataSource = dt;
這是我網格視圖移動到文本框
private void GridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow dr = GridView.SelectedRows[0];
id.Text = dr.Cells["id"].Value.ToString();
// or simply use column name instead of index
//dr.Cells["id"].Value.ToString();
judul.Text = dr.Cells["judul"].Value.ToString();
isi.Text = dr.Cells["subjek"].Value.ToString();
//textBox4.Text = dr.Cells[3].Value.ToString();
GridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
文本文件或文本框? – peter
你能發佈與textview,gridview和textbox相關的代碼嗎? – stackErr
我更新了我的帖子 –