Search code examples
c#formsdatagridviewopenfiledialogbindingsource

Does OpenFileDialog work with BindingSource?


I want to open a txt file with OpenFileDialog and add it to an datagridview in windows form.

I also want to use BindingSource because I have to add new data to the existing and loaded datagridview txt file from another Form using a button and some textboxes and the data given by the user in the textbox should be added to the datagridview to the existing txt file.

Here is my code of the input button event but I have no idea how to get started with another form and binding the data

public void input_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();

    ofd.ShowDialog();

    DataTable dt = new DataTable("TableName");
    //Set the Columns or fill it with a DataAdapter
    dt.Columns.Add("ID", typeof(string));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Display", typeof(string));
    dt.Columns.Add("Color", typeof(string));
    dt.Columns.Add("Capacity", typeof(string));
    dt.Columns.Add("Processor", typeof(string));
    dt.Columns.Add("Operation System", typeof(string));
    dt.Columns.Add("Released", typeof(string));

    dgvBindingSource = new BindingSource();
    dgvBindingSource.DataSource = dt;
    datagridview.DataSource = dgvBindingSource;
    dt.Dispose();
}

Solution

  • Looks like this is what you're looking for

     public void input_Click(object sender, EventArgs e)
     { 
     OpenFileDialog ofd = new OpenFileDialog();
    
            ofd.ShowDialog();
    
            DataTable dt = new DataTable("TableName");
            //Set the Columns or fill it with a DataAdapter
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Display", typeof(string));
            dt.Columns.Add("Color", typeof(string));
            dt.Columns.Add("Capacity", typeof(string));
            dt.Columns.Add("Processor", typeof(string));
            dt.Columns.Add("Operation System", typeof(string));
            dt.Columns.Add("Released", typeof(string));
    
            // Create Line with vars from Form to Write in the TxT File
            string LineAdd =
                TextBoxID.Text + ";" + //Write Var ID;
                TextBoxName.Text + ";" + //Write Var Name;
                TextBoxDisplay.Text + ";" + //Write Var Display;
                TextBoxColor.Text + ";" + //Write Var Color;
                TextBoxCapacity.Text + ";" + //Write Var Capacity;
                TextBoxProcessor.Text + ";" + //Write Var Processor;
                TextBoxOS.Text + ";" + //Write Var Operation;
                TextBoxReleased.Text + //Write Var Released;
                "\n"; // Create New Line And Finish Line
    
                System.IO.File.AppendAllText(ofd.FileName, LineAdd); //Add the line in File
    
            string[] file = System.IO.File.ReadAllLines(ofd.FileName); // Open the txt file
            for (int i = 0; i < file.Length; i++) //Loop for write values in DataTable
            {
                string[] line = file[i].Split(Char.Parse(";")); //Separates the vars using ";" Char
                dt.Rows.Add(line); //Add All vars in the DataTable
            }
    
            dataGridView1.DataSource = dt;
            dt.Dispose();
        }}