繁体   English   中英

C#Datagridview-如何组织和设置某些列和单元格值以及将数据保存在文本文件中

[英]C# Datagridview - How to organize and set certain column and cell values and save the data in a text file

我正在使用C3 VS 2012 Express。 具有带有选项卡控件的Windows窗体。 在一个选项卡上,我想要(并设置)一个datagridview(不确定这是否是我应该用来完成我所需要的东西,但看起来很适合-接受其他建议)。 请参考所附图片。 基本上,我需要创建一个文本文件,该文件将具有在所示的datagridview中设置和选择的设置。 图像参考目前,用户可以编辑该字段(对于某些字段,我希望保留该字段,而我已经标记了需要答案的区域。

  1. 如何向用户隐藏此索引,以及如何选择多个计算机名作为组名PLUTO的一部分。
  2. 我需要用户使用datetimepicker在此处选择日期和时间。
  3. 如何使按钮读为BROWSE并将值放置在位置单元格(或同一单元格)中编辑:我对此有部分答案。现在可以将值放置在位置单元格中:

     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; } 
  4. 我是否应该使用ADD按钮允许用户添加新行?

  5. 用户选择要删除的条目时,删除条目需要什么代码?
  6. 生成将用于写入文件(当前使用fileappend),但是我如何/如何指定要附加的元素(例如,列/行号)容易呢?

我终于通过大量的浏览以及太多深夜的方式解决了其中的大部分问题。 对于数字: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();
  1. 我设法使用添加按钮使用添加行:

     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 
  2. 为了删除行,我使用了一个按钮和以下代码:

     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); } } 
  3. 要删除一行,我使用了一个按钮和以下代码:

     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(); } } 

我希望这可以帮助正在奋斗的其他人。

最后一点..这可能不是BEST代码,但对我有用,我希望至少它也能使您如愿以偿。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM