locked
DataViewGrid RRS feed

  • Question

  • I have a DataViewGrid on my form and an edit button.
    The click event of the edit button calls a form.

    How do I pass to the form the selected row on DataGridView???
    What do I pass to the calling form?

     

    Thanks

    Tuesday, November 6, 2007 6:23 PM

Answers

  • Not very difficult, make your constructor method accep one int parameter, than pass the selected row index or the row itself:

     

    Option one:

    Code Block

     

    public partial class MyForm2 : Form

    {

    int m_RowIndex;

     

    public Form2(int RowIndex)

    {

    m_RowIndex = RowIndex;

    InitializeComponent();

    }

    }

     

    // From the Form1 you call the form like this:

    private void button1_Click(object sender, EventArgs e)

    {

    MyForm2 myForm2 = new MyForm2(dataGridView1.SelectedRows[0].Index);

    myForm2.Show();

    }

     

     

    Option Two:

     

    Code Block

    public partial class MyForm2 : Form

    {

    DataGridViewRow m_Row;

     

    public Form2(DataGridViewRow Row)

    {

    m_Row = Row;

    InitializeComponent();

    }

    }

     

    // From the Form1 you call the form like this:

    private void button1_Click(object sender, EventArgs e)

    {

    MyForm2 myForm2 = new MyForm2(dataGridView1.SelectedRows[0]);

    myForm2.Show();

    }

     

     

    Hope this is what you need.

    Regards,

    Fábio

    Tuesday, November 6, 2007 7:14 PM

All replies

  • I can't answer your last question but you can access the selected row on the datagridview a bunch of different ways.

    I believe your best bet would be to pass the Current object property of the BindingSource rather than accessing the value through the datagridview directly.
    Tuesday, November 6, 2007 6:28 PM
  • Do you have an example??

     

    Tuesday, November 6, 2007 6:50 PM
  • I strongly recommend reading into this further if you're planning on using databinding properly.

    Essentially your DataGridView is bound to a BindingSource.  The binding source provides a level of abstraction from your data model as well as provides a bunch of additional features to help improve data binding.  One of the Properties a BindingSource exposes is the Current object property.  This is the current position in the list of items your BindingSource is bound to.  In the case of being bound to a DataSet, it would be the DataRow of the DataTable your BindingSource's DataMember property is set to. It is not tied directly to a DataSet but rather to anything that implements the IBindingList interface.

    You can therefore access the BindingSource.Current object and cast it to whatever type it really is for further processing.
    Tuesday, November 6, 2007 7:10 PM
  • Not very difficult, make your constructor method accep one int parameter, than pass the selected row index or the row itself:

     

    Option one:

    Code Block

     

    public partial class MyForm2 : Form

    {

    int m_RowIndex;

     

    public Form2(int RowIndex)

    {

    m_RowIndex = RowIndex;

    InitializeComponent();

    }

    }

     

    // From the Form1 you call the form like this:

    private void button1_Click(object sender, EventArgs e)

    {

    MyForm2 myForm2 = new MyForm2(dataGridView1.SelectedRows[0].Index);

    myForm2.Show();

    }

     

     

    Option Two:

     

    Code Block

    public partial class MyForm2 : Form

    {

    DataGridViewRow m_Row;

     

    public Form2(DataGridViewRow Row)

    {

    m_Row = Row;

    InitializeComponent();

    }

    }

     

    // From the Form1 you call the form like this:

    private void button1_Click(object sender, EventArgs e)

    {

    MyForm2 myForm2 = new MyForm2(dataGridView1.SelectedRows[0]);

    myForm2.Show();

    }

     

     

    Hope this is what you need.

    Regards,

    Fábio

    Tuesday, November 6, 2007 7:14 PM