zzwyb89
zzwyb89

Reputation: 470

Updating textbox based on datagrid view

I have this datagrid view which links to the table answers in the database. The user can edit answers to questions in this form, but I'd like for the textbox to be updated as the button moves onto next answer. This is so the user can edit/delete stuff in the textbox and save it.

    private void NextQuestion_Click(object sender, EventArgs e)
    {
        QuestionsBindingSource.MoveNext();
    }

How can I get the textbox to refresh based on selected record in datagridview?

Upvotes: 0

Views: 134

Answers (1)

DonBoitnott
DonBoitnott

Reputation: 11025

Since you are using a BindingSource, you can get the Current object, cast it to it's type, and get a value.

Let's assume you are bound to a DataTable:

private void NextQuestion_Click(object sender, EventArgs e)
{
    if (QuestionsBindingSource != null)
    {
        QuestionsBindingSource.MoveNext();
        if (QuestionsBindingSource.Current != null)
        {
            DataRow row = (DataRow)QuestionBindingSource.Current;
            yourTextBox.Text = row["FieldYouWant"].ToString();
        }
    }   
}

What you cast Current to and the subsequent value reference are both dependent upon what you are bound to (what QuestionsBindingSource is). Adjust this example accordingly.

Upvotes: 1

Related Questions