none
how to get from sqlCommand object to something that can be used as dataSource for dataGridView? RRS feed

  • Question

  • Hello,

    It's been a while and I don't remember....

    I have a SQLCommand object all setup, along with the SQLConnection object. I just can't remember how to get from that to what I need to bind to a dataGridView object on a windows forms app? Do I have to create a dataset and tableAdapter?

    a quick rundown would be much appreciated.

    thanks!

    Thursday, March 25, 2010 10:23 PM

Answers

All replies

  • There is a pretty good walkthrough with sample code at http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx that shows how to hook up an adapter, DataSet, binding source and grid.

    The sample uses a string to initialize the adapter, but there is an overload that takes the SqlCommand if you already have that.

    • Marked as answer by c0pe Friday, March 26, 2010 12:25 PM
    Thursday, March 25, 2010 10:54 PM
  • Hi, thanks Marcelo. I assumed it would be the same as when I did for asp.net a few years ago, but I don't recall having to use a 'BindingSource' class before, I think I just directly assigned a datatable to a gridView's datasource property... is this 'BindingSource' required? is it new or has it always been that way for win forms databinding?

    thanks again!
    Friday, March 26, 2010 12:32 PM
  • The component has been there since .NET Framework 2.0, so you can still do data binding without it.

    However if you take a look at the class description at http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx, you'll see it has a number of handy features made accessible that otherwise happen behind the scenes, like support for the Current objects and a CurrentItemChanged event.

    • Marked as answer by c0pe Friday, March 26, 2010 5:38 PM
    Friday, March 26, 2010 5:17 PM
  • thanks again!
    Friday, March 26, 2010 5:39 PM