Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text in a DataGrid RowHeader? 1

Status
Not open for further replies.

andypandyswe

Programmer
May 4, 2005
17
SE
How can I get text in a RowHeader (in a DataGrid control)? I've set the ColumnHeaders (for ex. to "model") with:

dataSet.Tables["resultTable"].Columns.Add("model", typeof(System.String));

...but I can't figure out how to do the same with the rows???

I would be most grateful if someone would point me in the right direction...

P.S.
The table has no children or anything...
D.S.

Andreas
 
Thanx but I can't make it work...

I get an IndexOutOfBounds -exception for the following line:

int yDelta = dataGrid1.GetCellBounds(row, 0).Height + 1;

I can't say I understand the whole thing which makes it hard to figure out what's wrong...

Here is the whole solution code (it's line 8 that causes problems)

private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

{

int row = TopRow();

int yDelta = dataGrid1.GetCellBounds(row, 0).Height + 1;

int y = dataGrid1.GetCellBounds(row, 0).Top + 2;



CurrencyManager cm = (CurrencyManager) this.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];

while(y < dataGrid1.Height - yDelta && row < cm.Count)

{

//get & draw the header text...

string text = string.Format("row{0}", row);

e.Graphics.DrawString(text, dataGrid1.Font, new SolidBrush(Color.Black), 12, y);

y += yDelta;

row++;

}

}

 
Make sure that pointInCell00 is set in the forms Load event, not in the constructor.
 
Thanx a million SHelton, you've saved my a*s! It's working now...

I don't suppose someone knows how to get rid of the last row? There is always one empty row at the bottom with an asterisk (*) in the rowHeader column...

Thanx again

Andreas
 
You can remove the new row from the bottom of the datagrid by simply setting the ReadOnly property to true. However, if you want to remove the new row but still allow existing rows to be edited, you need to access the dataview of the datagrid, which has an AllowNew property:
Code:
((DataTable)dataGrid1.DataSource).DefaultView.AllowNew = false;
 
Thanks once again SHelton, I don't need to edit the rows so the first solution works great... However, two other problems have now cropped up :) Do you (or anyone else) have any ideas?

Problem 1:

The rowHeader text that ends up outside the window (the dataGrid is too large to be seen in it's entirety without scrolling) isn't there. If you expand the window downwards they appear but if you use the scrollbars to try to see the part of the dataGrid not in view, the rowHeaders are empty. The text doesn't appear until you click each of the affected rows.

Problem 2:

I'm trying to show four different values, one above the other, in each cell. I've solved this by combining the four values into a single string with \n inbetween the values. The problem is that in the cell that is active (one always is right from the start) the \n seems to disappear because the values are all on top of eachother and on a single line. When you move the focus to another cell (by klicking it) that cell's values get garbled and the previous cell is alright. When I set the dataGrid's "Enabled" -value to false the problem goes away but the ability to scroll goes away with it, and since the datagrid is to large to fit in the window, that's not a working solution.

Anyone?

Andreas
 
nobody? I the first problem is no biggie but the second one is too ugly...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top