Adding Row Numbers to DataGridView
-
I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.
int numberofrows = DG.Rows.Count;
int len = numberofrows.ToString().Length;
string NumberForRow;
for (x = 0; x < DG.Rows.Count; x++)
{
NumberForRow = Convert.ToString(x + 1);
while (NumberForRow.Length < len)
{
NumberForRow = "0" + NumberForRow;
}
DG.Rows[x].HeaderCell.Value = NumberForRow;
}This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.
-
I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.
int numberofrows = DG.Rows.Count;
int len = numberofrows.ToString().Length;
string NumberForRow;
for (x = 0; x < DG.Rows.Count; x++)
{
NumberForRow = Convert.ToString(x + 1);
while (NumberForRow.Length < len)
{
NumberForRow = "0" + NumberForRow;
}
DG.Rows[x].HeaderCell.Value = NumberForRow;
}This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.
Member 2256533 wrote:
int numberofrows = DG.Rows.Count; int len = numberofrows.ToString().Length; string NumberForRow; for (x = 0; x < DG.Rows.Count; x++) { NumberForRow = Convert.ToString(x + 1); while (NumberForRow.Length < len) { NumberForRow = "0" + NumberForRow; } DG.Rows[x].HeaderCell.Value = NumberForRow;
What length are you trying to get? this does not seem right. It looks you are trying to figure out how may digits are in a number. Is that right?
int len = numberofrows.ToString().Length;
int NumberForRow = 1;
foreach (DataGridViewRow row in DG.Rows)
{
if(NumberForRow < 10)
row.HeaderCell.Value = String.Format("{0:D2}", NumberForRow );
else
row.HeaderCell.Value = NumberForRow;NumberForRow = NumberForRow+ 1;
}Yusuf May I help you?
-
Member 2256533 wrote:
int numberofrows = DG.Rows.Count; int len = numberofrows.ToString().Length; string NumberForRow; for (x = 0; x < DG.Rows.Count; x++) { NumberForRow = Convert.ToString(x + 1); while (NumberForRow.Length < len) { NumberForRow = "0" + NumberForRow; } DG.Rows[x].HeaderCell.Value = NumberForRow;
What length are you trying to get? this does not seem right. It looks you are trying to figure out how may digits are in a number. Is that right?
int len = numberofrows.ToString().Length;
int NumberForRow = 1;
foreach (DataGridViewRow row in DG.Rows)
{
if(NumberForRow < 10)
row.HeaderCell.Value = String.Format("{0:D2}", NumberForRow );
else
row.HeaderCell.Value = NumberForRow;NumberForRow = NumberForRow+ 1;
}Yusuf May I help you?
True I am trying to find out how many digits are in the number so I can make the row headers all the same length. So if there are 1-9 rows the headers would look like 1,2,3,etc... and when there are 10-99 rows then 01,02...99, and 100-999 then 001,002,003...999 and this part works fine. The problem is that the row numbers don't show up at all in the first datagrid and they show up just fine in the second datagrid, using the exact same code.
-
I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.
int numberofrows = DG.Rows.Count;
int len = numberofrows.ToString().Length;
string NumberForRow;
for (x = 0; x < DG.Rows.Count; x++)
{
NumberForRow = Convert.ToString(x + 1);
while (NumberForRow.Length < len)
{
NumberForRow = "0" + NumberForRow;
}
DG.Rows[x].HeaderCell.Value = NumberForRow;
}This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.
An update to the problem. I took the code above and made a separte function out of it. Then Added the call to it after the GF.LoadDataGrid call with the same result. No numbers. Then I added a button to the form that called the same function. After the page was rendered and I clicked said button and the numbers appeared. So it seems that for some reason after the numbers are put there and the grid is rendered the row numbers disappear. Not sure why?
-
Member 2256533 wrote:
int numberofrows = DG.Rows.Count; int len = numberofrows.ToString().Length; string NumberForRow; for (x = 0; x < DG.Rows.Count; x++) { NumberForRow = Convert.ToString(x + 1); while (NumberForRow.Length < len) { NumberForRow = "0" + NumberForRow; } DG.Rows[x].HeaderCell.Value = NumberForRow;
What length are you trying to get? this does not seem right. It looks you are trying to figure out how may digits are in a number. Is that right?
int len = numberofrows.ToString().Length;
int NumberForRow = 1;
foreach (DataGridViewRow row in DG.Rows)
{
if(NumberForRow < 10)
row.HeaderCell.Value = String.Format("{0:D2}", NumberForRow );
else
row.HeaderCell.Value = NumberForRow;NumberForRow = NumberForRow+ 1;
}Yusuf May I help you?
For positive numbers this will do:
row.HeaderCell.Value = NumberForRow.ToString().PadLeft(requiredWidth, '0');
:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.
int numberofrows = DG.Rows.Count;
int len = numberofrows.ToString().Length;
string NumberForRow;
for (x = 0; x < DG.Rows.Count; x++)
{
NumberForRow = Convert.ToString(x + 1);
while (NumberForRow.Length < len)
{
NumberForRow = "0" + NumberForRow;
}
DG.Rows[x].HeaderCell.Value = NumberForRow;
}This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.
Something like this won't do? (The DataGRidView has an unbound column added at column 0 for the index)
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow R in dataGridView1.Rows)
{
R.Cells[0].Value = R.Index;
}
}EDIT: Oops. Never mind. Just noticed you want to put it in the row header cell....which doesn't work in the above code.
..and water fell from the sky like rain.
-
I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.
int numberofrows = DG.Rows.Count;
int len = numberofrows.ToString().Length;
string NumberForRow;
for (x = 0; x < DG.Rows.Count; x++)
{
NumberForRow = Convert.ToString(x + 1);
while (NumberForRow.Length < len)
{
NumberForRow = "0" + NumberForRow;
}
DG.Rows[x].HeaderCell.Value = NumberForRow;
}This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.