Solved

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

Posted on 2014-04-28
23
300 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
  • 13
  • 10
23 Comments
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
Do you have any code examples of this?
0
 

Author Comment

by:BlakeMcKenna
Comment Utility
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
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
Yes...
0
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:BlakeMcKenna
Comment Utility
Shaun,

Do you know how to do this?
0
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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
Comment Utility
Any ideas on this?
0
 
LVL 25

Expert Comment

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

Author Comment

by:BlakeMcKenna
Comment Utility
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 25

Accepted Solution

by:
Shaun Kline earned 500 total points
Comment Utility
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
Comment Utility
That worked but it eats up CPU time...but I might have to live with that!

Thanks for your help Shaun!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now