C#Datagridview-如何组织和设置某些列和单元格值,并将数据保存在文本文件中
我使用的是C3 VS 2012 express。使用带有选项卡控件的windows窗体。 在其中一个选项卡上,我想(并且已经设置)一个datagridview(不确定这是否是我应该用来完成我需要的东西,但似乎它适合-接受其他建议)。 请参阅附图。 基本上,我需要创建一个文本文件,其中包含在datagridview中设置和选择的设置。 目前,用户可以编辑该字段(我希望保留某些字段) 我已经标出了我需要答案的地方。 下面是:C#Datagridview-如何组织和设置某些列和单元格值,并将数据保存在文本文件中,c#,text,datagridview,C#,Text,Datagridview,我使用的是C3 VS 2012 express。使用带有选项卡控件的windows窗体。 在其中一个选项卡上,我想(并且已经设置)一个datagridview(不确定这是否是我应该用来完成我需要的东西,但似乎它适合-接受其他建议)。 请参阅附图。 基本上,我需要创建一个文本文件,其中包含在datagridview中设置和选择的设置。 目前,用户可以编辑该字段(我希望保留某些字段) 我已经标出了我需要答案的地方。 下面是: 如何对用户隐藏此索引,并能够选择多个计算机名作为组名PLUTO的一部分 我
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
if (fDialog.ShowDialog() != DialogResult.OK)
return;
System.IO.FileInfo fInfo = new System.IO.FileInfo(fDialog.FileName);
string strFileName = fInfo.Name;
string strFilePath = fInfo.DirectoryName;
string strFullFileName = fInfo.FullName;
textBox4.Text = strFullFileName;
//dataGridView1.Rows[e.RowIndex].Cells[3].Value = strFullFileName;
MessageBox.Show(strFileName + ", " + strFilePath + ", " + strFullFileName);
// Set value for some cell, assuming rowIndex refer to the new row.
dataGridView1.Rows[e.RowIndex].Cells[3].Value = strFullFileName;
}
通过大量的浏览和太多的深夜,我终于解决了大部分问题。 编号: 2.我决定不使用日期和时间,因为在这个阶段没有必要 3.使用以下方法添加了浏览按钮:
//this is for button click event for clicking a cell in DGV
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 7)
{
//MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
// choose file here
OpenFileDialog fDialog = new OpenFileDialog();
if (fDialog.ShowDialog() != DialogResult.OK)
return;
System.IO.FileInfo fInfo = new System.IO.FileInfo(fDialog.FileName);
string strFileName = fInfo.Name;
string strFilePath = fInfo.DirectoryName;
string strFullFileName = fInfo.FullName;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = strFullFileName;
dataGridView1.RefreshEdit();
private void button17_Click(object sender, EventArgs e) // add rows DGV
{
this.dataGridView1.Rows.Insert("one", "two", "three", "four","etc");
dataGridView1.Rows.AddRange();
} //end add rows DGV
private void button18_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count != -1)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
this.dataGridView1.Rows.Remove(row);
}
}
private void button19_Click(object sender, EventArgs e)
{
System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\foo\dgvTextFile.txt");//select name for created text file
try
{
string myTableLine = ""; //variable to hold each line
for (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)//loop through table rows, one at a time
{
//now loop though each column for the row number we get from main loop
for (int c = 0; c <= dataGridView1.Columns.Count - 1; c++)
{
myTableLine = myTableLine + dataGridView1.Rows[r].Cells[c].Value;
if (c != dataGridView1.Columns.Count - 1)
{
//set a delimiter by changinig the value between the ""
myTableLine = myTableLine + ",";
}
}
//write each line
file.WriteLine(myTableLine);
myTableLine = "";
}
file.Close();
System.Windows.Forms.MessageBox.Show("Grid Export Complete.All changes saved. Use create to create commands", "Program Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception err)
{
System.Windows.Forms.MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
file.Close();
}
}
private void按钮19_单击(对象发送者,事件参数e)
{
System.IO.StreamWriter file=new System.IO.StreamWriter(@“C:\foo\dgvTextFile.txt”);//为创建的文本文件选择名称
尝试
{
字符串myTableLine=“;//用于保存每行的变量
对于(int r=0;r