我将文本读取到GridView,然后将此文本从GridView移动到TextBoxes进行编辑。如何从选择行替换已更改的数据并保存到我的文本文件?
这是我的写文件
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;
}
答案 0 :(得分:0)
您应该在初始化DataGridView时调用GridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
,而不是在CellContentClick
期间调用SelectionChanged Event
。然后使用CellContentClick Event
代替CurrentCell.RowIndex
并使用private void GridView_SelectionChanged(object sender, EventArgs e)
{
DataGridViewRow dr = GridView.Rows[GridView.CurrentCell.RowIndex];
id.Text = dr.Cells[0].Value.ToString();
judul.Text = dr.Cells[1].Value.ToString();
isi.Text = dr.Cells[2].Value.ToString();
}
作为索引:
buton1_Click
您的wp_insert_user
会在文件末尾添加新值。我不确定这是不是你想要的。