1

I'm trying to Insert all Rows values of DataGrid for Once every Click of a button, so if user inserted three times display on datagrid three rows, I have a class there is code

public string Name { get; set; }
public string Job { get; set; }
public string Phone { get; set; }

public MyGrid(string Vendors,string Jobs,string Tel)
{
    Name = Vendors;
    Job = Jobs;
    Phone = Tel;
}

And I called into button click event here

static List<MyGrid> gride;
gride = new List<MyGrid>();
for (int i = 0; i < 3; i++)
{
    var myg1 = new MyGrid(textBox10.Text, textBox11.Text, textBox12.Text);
    gride.Add(myg1);    
}

dataGridView1.ItemsSource = gride;

This code it's working but there is the one problem. When add data is supposed to appear in a one row, but appears within 3 rows in the one click, I want to show one row in per click with different data . How I can add new row per click on the button in WFP?

3
  • Why are you looping it thrice then if you want it to be added only once? Remove the for loop then. Commented Jun 7, 2014 at 8:23
  • @RohitVats When i remove the for loop the data added only once right but when i write new data and i click add button the row data changed,i want to add new row not changed at the same row Commented Jun 7, 2014 at 8:30
  • There are couple of issues in your code apart from that. I have added an answer. See if that helps. Commented Jun 7, 2014 at 9:02

1 Answer 1

2

First of all this is not a way to do WPF way. Use proper bindings to achieve you want.

Steps to do in WPF way:

  1. Create ObservableCollection<MyGrid> and bind ItemsSource with that collection.
  2. Instead of setting ItemsSource again simply add object in that collection. DataGrid will be refreshed automatically since ObservableCollection implement INotifyCollectionChanged.

Now, for your code there are couple of issues.

  1. If you want item to be added once, why to run for loop and add object thrice. Remove the for loop.
  2. With every button click, you are re-initializing the list. Instead keep the list as instance field and initialize list only once from class constructor.
  3. No need to set ItemsSource again with every button click.

public class CodeBehindClass
{
   private ObservableCollection<MyGrid> gride;
   public CodeBehindClass()
   {
      gride = new ObservableCollection<MyGrid>();
      dataGridView1.ItemsSource = gride;
   }

   private void ButtonHandler(object sender, RoutedEventArgs e)
   {
      var myg1 = new MyGrid(textBox10.Text, textBox11.Text, textBox12.Text);
      gride.Add(myg1);
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you it's working now, but I have another question, how do I work looping them so I insert all rows within the database
You can use for loop then and run the loop equal to number of records in database and add them to the collection.
I have one question if you don't mind please how i can catch value cell like this dataGridView1.Rows[i].Cells[0].Value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.