views:

130

answers:

5

Hi,

Is there a way to turn on grid lines in the entire datagridview client area rather than them becoming visible as you add rows?

I have read the msdn but find nothing of use.

C#, winforms, visual studio 2008

Thanks, R.

A: 

You could add a heap of dummy rows to the grid and make them read only.

Gary
I did think about that but wanted to find out if there was a 'proper' way or a setting that could be used to 'turn on grid lines' rather than have to start dealing with dummy rows. But thanks, R.
flavour404
+2  A: 

You could possibly subclass the DataGridView, override its PaintBackground event and add an image of some lines. See here and here for some examples.

I realise this is a bit of a hack. :)

Andy
Andy, i'm leaning towards this answer just haven't had time to 'have a go at it yet,' will let you know the outcome.
flavour404
A: 

I faced that problem when I had number of rows in my datagrid what was taken from config file. In case of small number of the rows the datagrid looks ugly with gray background color. After discussion we desided to make datagrid height dynamic. That in its turn takes the parent window where the datagrid located to change its height as well. Now it looks pretty fine. This is not right answer to your question but rather consider style issue.

Arseny
A: 
int MAX_ROWS = 10;
int MAX_CELLS = 10;
dataGridView1.ColumnCount = MAX_CELLS;
int currentRowIndex = dataGridView1.Rows.Add(MAX_ROWS);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        cell.ValueType = typeof(String);
        cell.Value = "This is the: " + cell.OwningColumn.Index.ToString() + 
            " " + cell.OwningRow.Index.ToString() + " cell";
    }
}

I know that it's not the best solution but it doesn't require making of a new DataGridView-like custom control with setting it's background image.

virious
As I already discussed with Gary, below, I do not want to add a bunch of dummy columns and rows, but thanks for the suggestion.
flavour404
+1  A: 

I'd choose between Andy's proposal, and the virtual mode.

In the virtual mode of grid view you will need to provide an interface to the data store by handling a number of events from the control. In such case you can set a RowCount property directly and then decide in the CellValueNeeded event if the cell contains data or not. You will have to determine though the number of rows that will fill your control. http://msdn.microsoft.com/en-us/library/ms171622.aspx

ULysses
I need to look into the virtual mode as i have not encountered it before but at the moment I am leaning towards Andy's suggestion. As and when I get to implement it I will let you know the outcome. Thanks for the suggestions.
flavour404