Results 1 to 7 of 7

Thread: [2005] DataGridView Problems

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    31

    [2005] DataGridView Problems

    Dear all,

    How can i set the display cell type(eg, checkbox, button,text) in datagridview, that like, if the data from the database that is = 0, then checkbox is unchecked, if = 1 checkbox = checked?


    thanks!

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] DataGridView Problems

    If it's a boolean, use a checkbox column. Textual, textboxes. You have to define the columns based on the data you're going to receive.

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [2005] DataGridView Problems

    Quote Originally Posted by alvinso
    if the data from the database that is = 0, then checkbox is unchecked, if = 1 checkbox = checked?
    Try this:
    Code:
    DataGridView1.Rows(0).Cells(3).Value = Convert.ToBoolean(your_integer_value_goes_here)
    Make sure you assign proper Row/Col indexes. For column you can column name.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] DataGridView Problems

    If your data is coming from a database then you shouldn't have to worry about the type of the column in many cases. You should bind your DataTable to a BindingSource and then bind that to the grid. That will create a column in the grid automatically for each column in your DataTable. For most data types the column will contain TextBoxes, but for Boolean data it will automatically create a column of CheckBoxes.

    If you want something other than the default column type for your data, or you want additional columns that don't correspond to any data, then you need to do some extra work. For example, if you want a column of ComboBoxes then you need to set that up at design time. You add a ComboBox column to the grid and then set its DataPropertyName property to the name of the column in the data source you want it bound to. Now that column will be used when your data is bound instead of creating a default column.

    If you want a column of Buttons then you simply add a Button column. You would handle the grid's CellContentClick event to catch the Click event of each Button in the column.

    Once that's all done you should not access data in the grid directly. All navigation and data manipulation should be done via the BindingSource. The Current property of the BindingSource will return a DataRowView containing the data in the selected row.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] DataGridView Problems

    Quote Originally Posted by RhinoBull
    I don't believe in data binding even though .net has added a sort of different meaning to it.
    It doesn't take much to populate grid "manually".
    Let's say that you have a read-only grid to display data and a set of TextBoxes, etc. to update data. You would have to populate the grid, update the other controls when the grid selection changes, validate the data entered into the other controls, update the grid when the data was edited in the other controls, add new objects when rows are added and delete objects when rows were deleted. Data-binding can do all of that for you with essentially no code. I'm aware that there were arguments against using data-binding in VB6, but to the best of my knowledge those arguments were specific to the way VB6 implemented data-binding. It's quite different in VB.NET by virtue of the fact that ADO.NET is disconnected. Some may also argue that not using data-binding gives you more control, but I always use data-binding where possible and I have complete control over how my data and UI behave. To each their own, and I'd certainly not say that people must use data-binding, but I can't personally see an advantage to not. It's a big time-saver and cuts down on the amount of code you have to maintain.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [2005] DataGridView Problems

    No Jim, I'm well aware of what data binding is meant to do in .net and my point was not to bring long debates. The point was also made without any pre dot net experience (good or bad) in mind.
    As I said I do not believe in it and I'd rather do all of what you described and even more if I have to than have anything bound to anynthing in my programs.
    This way I can asure you it won't have any flaws in it.
    Data binding is a very bad thing to do regardless of how much it is promoted as "one of the most significant feature in today's RAD" - there is nothing in the world that would ever let buy into that.

    Cheers my friend.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width