• 0

[C#] WinForms DataGrid Help


Question

Im building a WinForms application and I have a DataGrid that is populated by binding to a DataTable. I want to be able to highlight the edits made in the DataGrid in red forecolour - so the user can easily see the changes that are waiting to be written to DB.

I have currently implemented this by creating a new column style class that inherits from DataGridTextBoxColumn and then overrides the Paint(...) method. I declare all the DataGrid columns to be of this column style (which messes up the DataGrid layout settings). Then whenever the user makes an edit i have to prefix it with square bracket so it knows its an edited cell and sets the forecolor red.

I dont really like this method... is there no simpler way to just show which cells have been edited... please advise... thx.

Link to comment
https://www.neowin.net/forum/topic/346207-c-winforms-datagrid-help/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

The datagrid included with VS.NET sucks, as I'm sure you're already realizing. There is a very decent open source grid available where that is very robust called SourceGrid. You may want to check it out. The learning curve is not bad at all. The downside is that it has no designer, so everything has to be done in code. Believe me, it's not that big of an issue compared to the pile of poo that is included with .NET

I know this doesn't answer your question directly. I'm mostly certain there is no other way to visually flag something that is edited with the DataGrid.

http://www.devage.com/

  • 0

first of all, before you start bashing the .NET datagrid control, you need to understand that this is a very versatile control, and is the most powerful and complex control in all of .NET.

second, you can accomplish almost anything with .NET's datagrid, provided you understand its "service model" well enough.

@$phinX:

the solution you describe is what i would also do. however, i don't understand why it would mess up the DataGrid layout settings. i use this technique in a lot of datagrids daily, and i have had 0 problems with that. just remember thay when you overwrite this paint() function, you need to do everything by yourself if you do not want to call the base class.

about sourcegrid: i use this grid as well pretty frequently, and i have to say that it has its benefits compared to .NET's datagrid, as well as some shortcomings.

if you don't need anything fancy, stick with .NET's datagrid

  • 0

SourceGrid sounds good... I will check that out for my own apps :yes: . Unfortunately this app im working on is a work application and i may not be able to justify the use of an external grid component.

I think ive almost got it sorted... Im now storing an ArrayList of the columns that have been edited. If a column has been edited i want to show the whole column in red forecolor.

I *thought* i could just set the DataGridTextBoxColumn forecolor to red for the required columns... but there is no forecolor property! The only place i found a forecolor property is on the DataGridTableStyle, which would make all columns red.

Any advice as to how to do this?

My other option is to use that method of overriding the Paint() method and check if the current colunm is in the ArrayList. But im having scope problems there... my inner class wont let me see public variables within that page of code! :wacko: Im goin mad here.... :blink:

  • 0
  CdCViRus said:
what you wanna do is in the Paint() function, check to see if the current column is in the ArrayList. if it is, then call g.FillRect() or something like that, before you draw the contents of the cell.

586256337[/snapback]

Yeah thats what im attempting but im now having problems with scope of objects. I have made a DataTable !public! yet i still cant access it in the inner class.

namespace RRI
{
  public class frmRR : System.Windows.Forms.Form
  {
    public DataTable dtResults = new DataTable();
    ...

    public class ColoredTextBoxColumn : DataGridTextBoxColumn
    {
      dtResults.rows(0) // CANT ACCESS dtResults HERE
    }

  }
}

Am i declaring that ColoredTextBoxColumn in the wrong place?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.