C# Winform: Textboxes to autoupdate contents of datagrid columns

N

Nevyn Twyll

Clear DayI have a windows form.
It fetches some data that it keeps in a Dataset.
I want to bind a grid to that datasource, and have 2 textboxes on the form
that display the contents of a couple of text fields.

The only thing I can really get to happen is to have the textboxes display
the data from the first row of the dataset, but not update.


Here's a sample of my code:

// Fetch the data and name the table
DataSet ds = sqlconnection.ExecuteDataSet(...)
ds.Tables[0].TableName = "MyTable";

// Bind the datagrid
myDataGrid.DataMember = "MyTable";
myDataGrid.DataSource = ds;

// Bind the textbox?
txtMyTextBox.DataBindings.Add(new Binding("Text", ds,
"MyTable.MyTextField"));

Thanks for the help!
 
M

Morten Wennevik

Hi Nevyn,

I managed to get it to update from the textbox using properties.

public string Field1
{
get
{
return (string)((dataTable1.Rows[0])[0]);
}
set
{
(dataTable1.Rows[0])[0] = value;
}
}

textBox1.DataBindings.Add("Text", this, "Field1");

However, if you edit the datagrid manually it will not update the textbox.


Happy coding!
Morten Wennevik [C# MVP]
 
N

Nevyn Twyll

Oh, I see I had a cruddy title.
It's all read-only. All I want is when the position of the current row in
the datagrid changes, I want the textboxes to show the values from that row,
but it never seems to change....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top