hi friends......
I need to print the row number in the row header of datagrid.what is the code for that?
plz help me......
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
naura paxRank #2,843 · 502 reputationPosted Sep 30, 2009, 3:02 AM EDT
one possible way is to add a column at position 0 of datagrid and
setting its value to rowindex+1 at RowAdded event.
Please mark as answered if helpful.
Resmy Ravi74 reputationPosted Sep 30, 2009, 3:27 AM EDT
I need to print in the row header itself...
is there any alternate solution for that?
Nir Bar107 reputationPosted Sep 30, 2009, 3:47 AM EDT
You can inherit DataGridViewRowHeaderCell and override it's Value member to be equal RowIndex + 1
You will need to set HeaderCell of every row to your custom type. You can do that in RowsAdded event of the DataGridView
Please mark this post as an answer if it helps you
Nir
Resmy Ravi74 reputationPosted Sep 30, 2009, 11:28 PM EDT
can you please give me the code?
Soumya Surendran84 reputationPosted Oct 1, 2009, 3:11 AM EDT
private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
DataGridView Gview = sender as DataGridView;
if (null != Gview)
{
Gview.Rows[e.RowIndex].HeaderCell.Value = Convert.ToString(dataGridView1.CurrentRow.Index+1);
}
}
William Clardy2 reputationPosted Oct 8, 2009, 2:15 PM EDT
int hpadding = 40;
Graphics graphic = gridview.CreateGraphics();
gridview.RowHeadersWidth = (int)graphic.MeasureString(gridview.RowCount.ToString("N0"), gridview.Font).Width + hpadding ;
graphic.Dispose();
gridview.RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight ;
foreach (DataGridViewRow dr in gridview.Rows)
{
dr.HeaderCell.ValueType = System.Type.GetType("System.String");
dr.HeaderCell.Value = (dr.Index + 1).ToString("N0");
}
I hope it helps.
(warning: I'm really a VB.Net kind of guy, so this may not be an optimal solution)