Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to delete Selected Rows from DataGridView with .txt file

$
0
0

Hello, to everyone,
I'm working on storage program, // in this case for books//,
And i have a bit of a problem with data, because its for university and my condition is to use *.txt files
so a little introduction...

class Books
    {
        public string bookAutor
        {
            get;
            set;
        }
        public string bookHeader
        {
            get;
            set;
        }
        public int bookCount
        {
            get;
            set;
        }
        public int bookPrice
        {
            get;
            set;
        }
        public static List<Books> LoadBooksListFromFile(string path)
        {
            var books = new List<Books>();
            foreach (var line in File.ReadAllLines(path))
	        {
		        var columns = line.Split('\t');
                books.Add(new Books
                    {
                        bookAutor = columns[0],
                        bookHeader = columns[1],
                        bookCount = Convert.ToInt32(columns[2]),
                        bookPrice = Convert.ToInt32(columns[3])
                    });
	        }
            return books;
        }
    }

Class witch fill my datagrid View from File

 private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = Books.LoadBooksListFromFile(@"C:\Users\Димитър\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\data\read.txt");
        }

On form load i Load the function and all data is show to dataGridView1 control

 private void button1_Click(object sender, EventArgs e)
        {
            StreamWriter sr = new StreamWriter(@"C:\Users\Димитър\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\data\read.txt", true);
            if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "" && textBox4.Text == "")
            {
                MessageBox.Show("Error");
            }
            else
            {
                sr.WriteLine(textBox1.Text + '\t' + textBox2.Text + '\t' + textBox3.Text + '\t' + textBox4.Text);
            }
            sr.Close();
            dataGridView1.DataSource = Books.LoadBooksListFromFile(@"C:\Users\Димитър\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\data\read.txt");
        }

And i have 4 textBoxex witch i fill with information and when i click i save them in //read.txt// and show it on DataGV.
So far is greath now i want to delete selected rows with button Delete.
But my DVG is bound and when i use this code:

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                if (dgvr.Selected == true)
                {
                    dataGridView2.Rows.Remove(dgvr);
                }
            }

it tell me that is impossible because of bounding..  other solution ive made is:

  int n = dataGridView2.Rows.Add();
            dataGridView2.Rows[n].Cells[0].Value = textBox5.Text;
            dataGridView2.Rows[n].Cells[1].Value = textBox6.Text;
            dataGridView2.Rows[n].Cells[2].Value = textBox7.Text;
            dataGridView2.Rows[n].Cells[3].Value = textBox8.Text;

Fill the data from 4 textBoxes
than when i use again 

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                if (dgvr.Selected == true)
                {
                    dataGridView2.Rows.Remove(dgvr);
                }
            }


this code is woring good, but in second case i cant load txt file in second DVG

Its mess in my head at the moment i ll really appreciate some help


Viewing all articles
Browse latest Browse all 2535

Trending Articles