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

C#Datagridview-如何组织和设置某些列和单元格值,并将数据保存在文本文件中,c#,text,datagridview,C#,Text,Datagridview,我使用的是C3 VS 2012 express。使用带有选项卡控件的windows窗体。 在其中一个选项卡上,我想(并且已经设置)一个datagridview(不确定这是否是我应该用来完成我需要的东西,但似乎它适合-接受其他建议)。 请参阅附图。 基本上,我需要创建一个文本文件,其中包含在datagridview中设置和选择的设置。 目前,用户可以编辑该字段(我希望保留某些字段) 我已经标出了我需要答案的地方。 下面是: 如何对用户隐藏此索引,并能够选择多个计算机名作为组名PLUTO的一部分 我

我使用的是C3 VS 2012 express。使用带有选项卡控件的windows窗体。 在其中一个选项卡上,我想(并且已经设置)一个datagridview(不确定这是否是我应该用来完成我需要的东西,但似乎它适合-接受其他建议)。 请参阅附图。 基本上,我需要创建一个文本文件,其中包含在datagridview中设置和选择的设置。 目前,用户可以编辑该字段(我希望保留某些字段) 我已经标出了我需要答案的地方。 下面是:

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

    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;
    
    
    }
    
  • 我应该使用ADD按钮来允许用户添加新行吗

  • 当用户选择删除条目时,删除条目需要什么代码
  • Generate将用于写入文件(当前使用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();
    
  • 我使用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
    
  • 要删除行,我使用了一个按钮和以下代码:

    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