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?

Answers


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.


Need Your Help

matlab: using function hints in .net assembly

c# .net matlab

I load c# assembly to matlab, there is hint like this to complete name of classes and function, but not arguments. Is there some workaround to show method signature?

How come in chrome I get a black background when trying to print but in IE it looks fine

html5 internet-explorer google-chrome printing-web-page

Does anyone know why I maybe getting black backgrounds in my article tags when I go to print my web page. The backgrounds are actually white and in internet explorer they look just fine. It displ...