Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:20:18+00:00 2026-06-11T15:20:18+00:00

I’ll try to put this as clearly as I can, but I’m all too

  • 0

I’ll try to put this as clearly as I can, but I’m all too aware that I may be asking the wrong question. I have a datagrd in WPF (.NET 4.0), which I bind to a datatable in a dataset. The relevant code-behind (running on window load) is:

// create a connection to Top Trumps database
String cs = ConfigurationManager.ConnectionStrings["csTopTrumps"].ConnectionString;
SqlConnection cn = new SqlConnection(cs);

// create a new dataset
DataSet ds = new DataSet();

// open the connection (not strictly necessary, as the
// data adapter will do this when you use it anyway)
cn.Open();

// fill data table called Cards with contents of tblCard
SqlDataAdapter daCards = new SqlDataAdapter();
daCards.SelectCommand =
new SqlCommand("SELECT * FROM tblCard ORDER BY CardTitle", cn);
daCards.Fill(ds, "Cards");

// now set the data context for the entire window
this.DataContext = ds;

My datagrid definition begins:

<DataGrid ItemsSource="{Binding Tables[Cards]}"

All of that works fine, and I understand it. I now want to make the data-binding two way, so that changes made in the datagrid will update back to the underlying database. I know that one way I can do this is by getting at the values of changed rows and using a tableadapter with an Update method to write any changes made back to the underlying database (perhaps on something like the SelectedCellsChanged event, or similar). However, any approach like this is difficult to implement, because getting at cell values in code-behind in WPF is difficult (and my understanding is that this is the wrong way to go, as I should be using the WPF framework, so help me).

So … how can I set two-way data-binding to do the work for me? I’ve seen people mention setting datagrid properties like:

SelectedItem="{Binding Path=SelectedIndex,Mode=TwoWay}"

but I don’t understand how I could get this to work with data adapters and datasets. What I don’t want is any solution involving MVVM or models, as I’m not using these (and yes, I probably should be, I know). I also want to avoid reams of complicated C# code, involving observable collections, recursive searches through the control hierarchy to find parents and suchlike.

I’ve searched for a long time, and I THINK what I’m asking does exist – if not, please tell me it doesn’t, and apologies for wasting your time, gentle reader!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:20:19+00:00Added an answer on June 11, 2026 at 3:20 pm

    OK, a bit late, I think I have the answer I wanted. The datagrid is bound to a data table (in my instance). This means you can get at the selected item (which is the row a user double-clicked on, perhaps), turn it into a datarow and then get at the field you’re interested in.

    private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    

    {

    // turn the datagrid selected "item" into a data row
    DataRowView dr = dg.SelectedItem as DataRowView;
    
    // get at the value for any field (here it's the one called CARD) in the underlying datatable
    string CardTitle = (string)dr["Card"];
    
    // display it ...
    MessageBox.Show("You are on card " + CardTitle);
    

    }

    This will solve the problem of double-clicking on a row in a datagrid, and is what I had in mind. If you’re using classes, you can cast the selected item into the relevant object, and get at its properties.

    I hope this helps someone!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.