C#数据网格视图-如何组织和设置某些列和单元格值,并将数据保存在文本文件中

本文关键字:数据 存在 文件 文本 单元格 保存 视图 网格 数据网 何组织 设置 | 更新日期: 2023-09-27 18:29:03

我使用的是C3 VS 2012 express。有一个带有选项卡控件的windows窗体。在其中一个选项卡上,我想(并设置了)一个数据网格视图(不确定这是否是我应该用来完成我需要的任务,但似乎很适合-可以接受其他建议)。请参阅附图。基本上,我需要创建一个文本文件,该文件将具有在显示的数据网格视图中设置和选择的设置。图像参考目前用户可以编辑字段(我想保留一些字段我已经标出了我需要答案的地方。如下:

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

    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. Generate将用于写入文件(目前使用fileappend),但我如何指定要附加的元素(例如列/行号)

C#数据网格视图-如何组织和设置某些列和单元格值,并将数据保存在文本文件中

我通过大量的浏览和太多的深夜终于解决了大部分问题。对于编号: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. 我设法使用add按钮添加行,使用的是:

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

我希望这能帮助其他正在战斗的人。

最后一点。。这可能不是最好的代码,但它对我有效,我希望如果你也在战斗,至少它会让你继续前进。

相关文章: