Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,i have the following code that creates a datagridview control and put it into a tab page of a tabcontroll, my problem is that i need to get somehow (mostlikely with an event handler (because i need allways to get the realtime selected row)) a value from a selected row in this data grid view to be used in another button. Anyone know how can i do this?

C#
private void button1_Click(object sender, EventArgs e)
        {
            tabControl1.TabPages.RemoveByKey("Database ImageTable");
            DataGridView datagrid1 = new DataGridView();
            Utile.Customtabpage.tabpage(tabControl1, datagrid1, "Database ImageTable");
            datagrid1.DataSource = Utile.DB.fillImgT(ConnectionSTR);
            datagrid1.BackgroundColor = Color.White;
            datagrid1.AllowUserToAddRows = false;
         
        }

//I know that my english is kinda bad so i`m sorry for that.
With respect, Aryx.
Posted
Updated 31-Mar-12 8:43am
v2

1 solution

Hello

Try this:

C#
private void button1_Click(object sender, EventArgs e)
        {
            tabControl1.TabPages.RemoveByKey("Database ImageTable");
            DataGridView datagrid1 = new DataGridView();

            datagrid1.SelectionChanged += new EventHandler(datagrid1_SelectionChanged);

            Utile.Customtabpage.tabpage(tabControl1, datagrid1, "Database ImageTable");
            datagrid1.DataSource = Utile.DB.fillImgT(ConnectionSTR);
            datagrid1.BackgroundColor = Color.White;
            datagrid1.AllowUserToAddRows = false;
         
        }


Then Define a Method:
C#
void datagrid1_SelectionChanged(object sender, EventArgs e)
{
    var rows = ((DataGridView)sender).SelectedRows;

    if (rows.Count > 0)
    {
        //...
    }
}
 
Share this answer
 
v3
Comments
aryx123 1-Apr-12 1:58am    
Thank you, its very usefull.
Shahin Khorshidnia 1-Apr-12 8:21am    
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900