How to get datagridview current row columns value from binding source in c#

NazHim 206 Reputation points
2020-12-26T04:40:26.997+00:00

Hi All

How to get datagridview current row column value from binding source in c#

i am using C# windows form

anybody can help me! with provide some code snippet

Developer technologies Windows Forms
Developer technologies Universal Windows Platform (UWP)
{count} votes

3 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2020-12-28T02:51:35.44+00:00

    Hi NazHim,
    Do you want to get a specific value from the BindingSource and not from the DataGridView control?
    If so, you can use BindingSource.Current property to to access the current item.
    Here is a code example:

    public BindingSource bindingSource1 = new BindingSource();  
    private void Form1_Load(object sender, EventArgs e)  
    {  
        PopulateBindingSourceWithFonts();  
    }  
    private void PopulateBindingSourceWithFonts()  
    {  
        bindingSource1.CurrentChanged += new EventHandler(bindingSource1_CurrentChanged);  
        bindingSource1.Add(new Font(FontFamily.Families[2], 8.0F));  
        bindingSource1.Add(new Font(FontFamily.Families[4], 9.0F));  
        bindingSource1.Add(new Font(FontFamily.Families[6], 10.0F));  
        bindingSource1.Add(new Font(FontFamily.Families[8], 11.0F));  
        bindingSource1.Add(new Font(FontFamily.Families[10], 12.0F));  
        DataGridView view1 = new DataGridView();  
        view1.DataSource = bindingSource1;  
        view1.AutoGenerateColumns = true;  
        view1.Dock = DockStyle.Top;  
        this.Controls.Add(view1);  
    }  
    void bindingSource1_CurrentChanged(object sender, EventArgs e)  
    {  
        MessageBox.Show(bindingSource1.Current.ToString());  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Bonnie DeWitt 811 Reputation points
    2020-12-31T23:40:32.337+00:00

    Hi NazHim,

    You didn't say what type of DataSource you're binding to, but I have an example of what you can use if you're binding to a DataTable or DataSet. Otherwise, you should be able to tweak this example for your situation.

    // The only way that I know off to get PropertyName of the data in the current cell of the DataGridView
    string colName = this.dataGridView1.CurrentCell.OwningColumn.DataPropertyName;
    
    // And we have to use an object for the value of the DataRow column 
    object colValue = null;
    
    // Now, let's find the value that's in that cell, where this.bs is the BindingSource:
    if (this.bs.DataSource is DataTable)
        colValue = ((DataTable)this.bs.DataSource).Rows[this.bs.Position][colName];
    else if (this.bs.DataSource is DataSet)
    {
        DataTable dt = ((DataSet)this.bs.DataSource).Tables[this.bs.DataMember];
        colValue = dt.Rows[this.bs.Position][colName];
    }
    

    That's it. That should work for you!


  3. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-01-02T16:03:53.347+00:00

    Hello @NazHim

    If you still need assistance see the following where the first example works off a list and the second example off DataTable containers.

    Using classes

    In the following code sample data is read from a database via Entity Framework, placed into a DataGridView. Each row uses this class (which is known as a projection). Data is loaded here and on line 33 code to get the current row. The code to get the current row is in a language extension here.

    52777-dgv2.png

    Using DataTable

    The following code sample (see here) shows how to get a BindingSource.Current values where in this case this is a master detail setup using DataTable containers.

    In the screenshot there is a TextBox which displays current column details.
    52749-dgv1.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.