?
Solved

Adding a DataGridViewCheckBoxColum to a DataGridView except for 1st row?

Posted on 2014-04-28
23
Medium Priority
?
309 Views
Last Modified: 2014-05-06
Is it possible to add a DataGridViewCheckBoxColumn to a DataGridView  omitting the 1st row of the grid? I'm thinking it is but it would have to be done on a row by row basis of which I'm not exactly sure how to do...any ideas?

Thanks!
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 10
23 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40030969
The OnRowDataBound Event fires for each row in the underlying data source. You can use this event to control things like what is displayed in a column or special formatting of text (such as coloring negative dollar amounts red). To handle the checkbox column, you may want to add the column and then convert it into a custom template. Once converted, you can give the generated control in the template column a specific name and reference it in code using the FindControl method on the DataGridRow object.
0
 

Author Comment

by:BlakeMcKenna
ID: 40031967
Do you have any code examples of this?
0
 

Author Comment

by:BlakeMcKenna
ID: 40035378
Ok,

I'll try and simplify this. I'm thinking in the CellFormatting Event, I just want to change a specific cell's type from a CheckBoxCell to a TextBoxCell. How can I do this?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40035591
If you use the CellFormatting event, the event will fire for each cell in the grid. This could cause performance problems. I also do not believe you can change the column's type of a single cell in the column.

To understand your original request, you want a column in your grid to display checkboxes but have the first data row to display a textbox instead? What will the textbox be used for?
0
 

Author Comment

by:BlakeMcKenna
ID: 40035603
There is logic in the CellFormatting Event that occurs only when the specific cell is encountered.

The textbox will just be an empty cell that can't allow for data input or receive data. It's basically a "dummy" cell.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40035624
So you want to hide the first row of data returned from your data source? Could you add a dummy row either in your data source query, or when the data is returned but prior to binding the data source to your grid?

Or is it that you do not want your user to be able to interact with the checkbox in the first row?
0
 

Author Comment

by:BlakeMcKenna
ID: 40035637
This grid is built manually, it's not a data bound grid. Plus, there is only one cell in the 1st row that I need to make this change to...the last cell in row 0. I basically just don't want a checkbox in that cell.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40035658
By manually building the grid, do you mean you are manually adding rows to the grid and manually setting what text/controls appears in each cell in the grid?
0
 

Author Comment

by:BlakeMcKenna
ID: 40035779
Yes...
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40037086
In the code that is manually adding rows and setting the text/controls, would you not be able to make a provision that if you are adding the first row to the grid, that you would not add a checkbox to the last column?
0
 

Author Comment

by:BlakeMcKenna
ID: 40037711
I'm down with that! I'm just not sure how to do that. I know how to add a checkbox column....I'm just not sure how to add it to a single cell...
0
 

Author Comment

by:BlakeMcKenna
ID: 40041942
Shaun,

Do you know how to do this?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40042149
You are confusing adding a checkbox to a cell with adding a checkbox column to a GridView. When you add a checkbox column, all cells in that column will have a checkbox, which is not your desire. If you are manually adding rows, you would need to create a checkbox control, set its properties and then add it to the cell using the cell's Controls.Add method.

Manually building a GridView is probably not the preferred method. Generally speaking, you would normally fill a DataTable object (either manually or using a data source) and then bind that DataTable to the GridView. By this method, you can separate out the UI (which is normally set at design-time) from the code that populates the data in the grid. For instances like what you need, you would use the OnRowDataBound event to change the visible property of the checkbox to false for that first row when it is bound.
0
 

Author Comment

by:BlakeMcKenna
ID: 40042264
Unfortunately, I have to build this grid manually but I agree with you that it's not the best or preferred way of doing it.

What is the syntax to add a checkbox to the cell. I couldn't find the "Controls" Method for the Cells property.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40042376
The syntax can be found at this URL on Microsoft's website:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.add(v=vs.110).aspx

This link provides a code example for both C# and VB.Net.
For your purposes, you need to reference the cell directly. It would be in this format:
GridView1.Rows[x].Cells[y].Controls.Add(<your created checkbox control>)

Where GridView1 is your grid control's name, x is the row index and y is the column index.

If you are creating a DataGridRow, it should have a cells collection and you can do:
<Your row object>.Cells[y].Controls.Add(<your created checkbox control>)
0
 

Author Comment

by:BlakeMcKenna
ID: 40042405
Have you tried to test this statement?

I've tried this and like I said in previous post...there is no "Controls" method for the Cells property. Based on the syntax your using....this method does not exist.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40042685
I've been leading you down the wrong path (sorry), and it appears that the solution is simpler than I imagined. You need to add the Checkbox column to your grid. When you manually add your rows, you should be able to hide the checkbox in the desired row as such:
<your grid view>.Rows[x].Cells[y].Visible = false;

Or you can make it read only:
<your grid view>.Rows[x].Cells[y].ReadOnly = false;
0
 

Author Comment

by:BlakeMcKenna
ID: 40042809
That makes sense and is definitely easier, however, I got the following error in the code-behind (see screenshot).
Screenshot.jpg
0
 

Author Comment

by:BlakeMcKenna
ID: 40044914
Any ideas on this?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40045479
Hmmm...How about setting the value of that cell to null/nothing?
0
 

Author Comment

by:BlakeMcKenna
ID: 40045612
I tried that and all it does is uncheck the checkbox...it's still appears as a checkbox instead of a blank cell.
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 1500 total points
ID: 40045633
Seems kind of extreme, but it appears that you will need to override the CellPainting Event for that cell. Here's a example from Microsoft's forum website:

http://social.msdn.microsoft.com/Forums/en-US/9e817c73-9849-498a-99ee-54e7592732a1/how-to-hide-checkbox-in-particular-cell-in-datagridview-checkbox-column?forum=vbgeneral
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40045656
That worked but it eats up CPU time...but I might have to live with that!

Thanks for your help Shaun!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question