.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

DataGridView & Custom cell painting

Posted By:      Posted Date: October 03, 2010    Points: 0   Category :Windows Application

I'm creating a schedule using a datagridview.  The cells in the grid need a lot of special behavour so I created my own Column and Cell classes.  In the Cell.Paint method I draw the cell text in various colors, draw images and lines, depending on data values.

My question is this:  What is the correct order to draw parts of the cell using the DataGridViewPaintParts?  Does it matter?  At present, the display is horrid - sometimes the cells are transparent (I can see what's underneath the application) and the text is scrambled.  A really good example of how to paint a cell would be great!


View Complete Post

More Related Resource Links

Painting/Filling a specific cell using CellPainting event of datagridview



I am trying to paint a quantity column in data grid with color red, however when I do so cell alignment changes to left. 

Seems like I have to convert e.value to string in order for this to work and when I do that the cell alignment changes.

Quantity column is of type decimal.

Also I would appreciate if you can guide on how i can fill the certain columns in data grid with certain color(for ex. yellow).

 if (this.dtaGridView.Columns[7].Index ==e.ColumnIndex && e.RowIndex >= 0)




Presenting Data with the DataGridView Control in .NET 2.0 > Defining Custom Column and Cell

in this link u can find the explanation about how to bind image column at run time.

Custom Editing Behavior for DataGridView TextBox Columns

For various reasons I'm using a DataGridView instead of a ListView, and while configuring the DataGridView to look like a ListView wasn't much of a problem, there's one thing that got on my nerves, which is the behavior of textbox cells in edit mode: It is much too easy to leave the edit mode accidentally, simply by pressing the cursor keys at the wrong time. For example when the text caret is positioned behind the last character of the textbox cell content, and you press the right arrow key

Datagridview painting "EXTREMELY SLOW" on new PC

Hi, I have a appliation I wrote in .net 2.0 (VS 2005) and on all PCs except one it runs great, but on this one laptop (a brand new Dell XPS 1530) the Datagridview paints so slow when scrolling or resizing that you can see each row paint as if in slow motion.  It's so bad I would be extremely hesitant to relase the application as I know someone would complain.   Has anyone else seen this issue?  It's only happening on certain PCs.  The PCs in question are all up to date via windows update, so they have the same versions of everything.  The laptop has a Nvidia 8600GT chip in it with the latest drivers and I actually have the same drivers on some of the PCs where the grid is fast.   Thanks,   Tony Caduto AM Software Design Home of Lightning Admin for PostgreSQL and MySQL http://www.amsoftwaredesign.com

How to make a DataGridView Cell/Column not enterable / selecatble

Hi, I am trying to make a Column in a DataGridView non-entaerable (the user should not be able to set focus to the cell neither using the mouse nor the keyboard). I already tried to change the CurrentCell property in the CellStateChanged. But in this event-handler the CurrenCell is the "old" one; setting it to any value doesn't help, because the value is set after calling this procedure. In Delphi there is an event-handler for entering a cell, where you can tell whether the user is allowed to enter a cell. Is there anything comparable in C#? Any other suggestions?

SSRSReport's cell while in custom webpart behaving mischieve

Hi   We have ended up with a new problem for which I not able to find answers through googling.   We are using Sharepoint 2007 and SSRS 2008.     We develop reports using SSRS 2008 and then deploy reports into Reporting services manager which is configured in native mode.   Now we have a dashboard designed in sharepoint which contain n number of webzones. We have also created a   custom webpart to display reports. So in each of the webzones we will add our custom webpart. This custom   webpart has got property where I can mention report service manager path and report path. So that these   reports are shown through this custom webparts.   The reason why we did not use built in report viewer was when clicked on some reports we wanted to be navigated to different pages with paramater provided on URL and loading dasboard page will have some reports in different webparts each of reports in webpart will read the parameter from URL which I dont how to do from built in reportviewer.   Now what is happening when I add few reports to custom webparts. Some of the cells of some reports does not show border colors. some cells in some reports are having text misaligned, text is having different font than what we actually set for that.   When we opened the view source, we found that two different reports which are displayed in

datagridview select cell


okay, this is driving me crazy!!!

In my datagridview, on the current row, how do I select/set the focus of a particular cell?  I am already on the row I just want to set the focus to the third cell.


after I validate a cetain cell, I want to tab to the next cell

I tried:


SendKeys.Send("{TAB}")  and the application stalls


I tried:


DgvSched.CurrentCell = DgvSched[3, DgvSched.CurrentRow.Index];

and also

DgvSched.CurrentCell = DgvSched[3, e.RowIndex];

and I get the following error:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll


when I remove the above statement the error does not come up.




Find out if DatagridView Cell is Clicked


I have 2 grids on a form.

Both grids are binded to object Panel and PanelLines.

If user click first panel grid, the second panel line grid will show the related panel lines.

If the user clicks the panel grid on the same cell again, I want panel line grid to scroll rows as such visible rows hide and bottom rows show up.

The following code does the scrolling part.

dgvPanelItem.FirstDisplayedScrollingRowIndex = dgvPanelItem.DisplayedRowCount(False) - 1
But I need help in finding out, if first grid ( panel grid ) is already clicked or not.

Find out if DatagridView Cell is Clicked


I have 2 grids on a form.

Both grids are binded to object Panel and PanelLines.

If user click first panel grid, the second panel line grid will show the related panel lines.

If the user clicks the panel grid on the same cell again, I want panel line grid to scroll rows as such visible rows hide and bottom rows show up.

The following code does the scrolling part.

dgvPanelItem.FirstDisplayedScrollingRowIndex = dgvPanelItem.DisplayedRowCount(False) - 1
But I need help in finding out, if first grid ( panel grid ) is already clicked or not.

handle up & down keys while editing a cell in datagridview to affect listbox


I have a datagridview. i have overridden some datagridview methods to capture enter key and updown key in a separate class



i also have a listbox. while i am editing a cell the listbox is being visible and searching in the listbox for what is in the cell's textbox. (i am also able to catch the up down key but from a different class which overrides some methods.)

Now what i need is when i press up and down key the listbox's selected items will change accordingly.

Plz help me to solve it.

Small problem with displaying custom calendar cell text from db in Safari


using VS2010

I'm using the calendar control to create a simple event calendar. I want to add a small bit of custom text just below the date of any cell that contains an event on that date.

The code:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
      string connString = ConfigurationManager.ConnectionStrings["CalendarConnectionString1"].ToString();
      SqlConnection myConnection = new SqlConnection(connString);
      SqlDataAdapter myAdapter = new SqlDataAdapter("GetEvents", myConnection);
      DataSet myDataSet = new DataSet();
      myAdapter.Fill(myDataSet, "Table");
      //If the month is CurrentMonth
      if (!e.Day.IsOtherMonth)
        foreach (DataRow dr in myDataSet.Tables[0].Rows)
          if ((dr["Date"].ToString() != DBNull.Value.ToString()))
            DateTime dtEvent = (DateTime)dr["Date"];
            if (dtEvent.Equals(e.Day.Date))
              e.Cell.BackColor = Color.MediumAquamarine;
              Label label = new Label();
              label.Text = "</br>";
              label.Text += dr["Title"].ToString();              

C# Datagridview cell text compare



I have 2 datagridviews on a form and would like to know if it is possible to compare the text in the 2 views and highlight any differences?

For example:

DG1: Cell A1 Text > This is a test

DG2: Cell A1 Text > This iss some test

Is it therefore possible to highlight "iss" , "some" in red and show the corresponding dgv1 text in green?

I have done this with 2 richtextboxes and cannot work out how to do this with datagridview?

Any help with this is most welcome

Thanks in advance

DataGridView Combo Box Cell with sub menu


I need to create a column in the datagirdview that functions like a combo box with sub menus. As an example we have a cell that contains modes of transportation, and rather then a single list to select from the user can choose cars and then get a sub menu of car types or they can choose trucks and get a sub menu of trucks to choose from. 

Modes of Transportation
cars -> Volkswagon Rabbit, Volkwagon Dasher, Volkswagon Bug
truck -> Ford F150, Toyota etc . . 

I hope I have explained my problem accurately enough, if anyone has any insights or suggestions I would love to hear them.




enabling the cursor in a specific cell within a datagridview


guys, i have an add record button in my datagridview application

i would like to enable the cursor on the first cell (in the last row) so the user would be able to enter some more information

i created an on click event, and can get and select the last row

but how do i set the cursor inside the specific cell ?

  Private Sub TestButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TestButton.Click
        Dim RowCount As Integer = Me.grdAccountTypes.RowCount - 1
        Me.grdAccountTypes.FirstDisplayedScrollingRowIndex = RowCount
        Me.grdAccountTypes.CurrentCell = grdAccountTypes.Rows(RowCount).Cells(0)
End SUb

how to update cell in datagridview ?



i have datagridview that connect to my database (access)

if i stay on any cell and change the value, a see that the value is changed

but when i make refresh i see that the value is back to the original value.

how i can update this cell (without sql query)

i bind dataset to datagridview like this:

dsView = new DataSet();
= new OleDbDataAdapter("SELECT * FROM Items", Conn);
.Fill(dsView, "Items");

OnPaint custom activity painting


what is the procedure of painting the activity layout in designer? i want paint activity way I like. i have overriden onpaint methods in designers of activities. it seems to work for activities which arent placed inside others ( composite ). when i place some activities inside composite activity then inner activites arent repainted. .. please give me some clue what is a procedure here ..




c# 2008 Datagridview numericupdown cell not working properly. Odd layout behaviour


Hi all,

Francesco from Italy here, working with MS products since I was professionally born... that's like a century ago or something like that!

This is my 1st post in MSDN forums, so I hope it would be well accepted :)


I'm working with datagrdiview and custom numericupdown cell. I started from here


So, I downloaded the code attached there, opened the solution with Visual studio 2008, processed the automatic upgrade from 2005 to 2008 and started to play a bit with it.

It works pretty well, since I tried to use it the way you can see in this project


What do I do?

- I have a MDI form containing a simple child form 

- the child form has a splitcontainer, containing a treeview in the left panel

- the treeeview has 4 nodes and, clicking node 1 or 2, loads an usercontrol, containing datagridview with the custom numericoupdown object.

It looks great!

But... if you play a bit with the fields, you'll get an odd behaviour.

The first object I interact with works pretty well. The second object (hosted by the other usercontrol) has a funny behaviour... It simply doesn't work properly.

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend