Umesh_karthy_41cdd8

hi

my application uses a datagrid. i generate rows dynamically as per my requirment and add values to them.
is there any means by that i can name or number the row sequentially
i.e., rows 0,1,2... and so on.

this is the code snippet where i generate the rows and add it to the tables.

for(int i = 0; i < x; i++)
{
newRow1 = table.NewRow();
rowCount++;
// Add the row to the Customers table.
table.Rows.Add(newRow1);
}

thanks


Re: Visual C# General numbering rows!!

TilakGopi

Hi,

I feel, you wrote the right code.

Is that code not generating rows sequentially

Seeing the example given at

http://msdn2.microsoft.com/en-us/library/system.data.datatable.newrow.aspx

may clear your problem.

Thanks & Best Regards,

Ch.T.Gopi Kumar.






Re: Visual C# General numbering rows!!

Umesh_karthy_41cdd8

it is generating rows sequentially only. but i'm unable to name it.
that s my problem.




Re: Visual C# General numbering rows!!

TilakGopi

Hi,

sorry, i didn't get u what did u mean by ' but i'm unable to name it'.

DataRow has no property as such.

Thanks,

Ch.T.Gopi Kumar.






Re: Visual C# General numbering rows!!

Umesh_karthy_41cdd8

i want to display number sequentially.
say, like row1 --1
row2 -- 2

like serial number.,




Re: Visual C# General numbering rows!!

TilakGopi

Hi,

Where do you want to display so

In the first field of row

Thanks,

Ch.T.Gopi Kumar.






Re: Visual C# General numbering rows!!

VictorC

Well since you create them sequentially, they get stored in the datagrid sequentially, and can be displayed sequentially [the index of the datagrid for each row is the 'number/name' that you have been looking for]

From what I understand you want the rows displayed sequentially, as in sorted sequentially in your datagrid Well you could store that number as a value in the row [simply create a new column and have the index as its value] and set the column to hidden in your datagrid [so you can access the number but it wont be displayed] this is rather redundant though
Then you would sort your datagrid based on this hidden column.




Re: Visual C# General numbering rows!!

Umesh_karthy_41cdd8

this redundant model was ruled out by the designers.
:-)






Re: Visual C# General numbering rows!!

Umesh_karthy_41cdd8

yes.
in the first field only.




Re: Visual C# General numbering rows!!

TilakGopi

Hi,

Then make the for loop as


for (int i = 0; i < x; i++)
{
newRow1 = table.NewRow();
//First field of table must be of type string


//newRow1[0] =
"row" + (i + 1).ToString() + "--" + (i + 1).ToString;

//or - more preferrable way is

newRow1[fieldname] = "row" + (i + 1).ToString() + "--" + (i + 1).ToString;

rowCount++;

// Add the row to the Customers table.

table.Rows.Add(newRow1);
}



That's it.Are u done

Thanks & Best Regards,

Ch.T.Gopi Kumar.






Re: Visual C# General numbering rows!!

Umesh_karthy_41cdd8

no... unfortunately i'm not done...
i dont want to dedicate a seperate column for this purpose.
it has to appear in the space called "Row Header".


that s the extreme left column. that appera naturally, even if u dont add a single column.





Re: Visual C# General numbering rows!!

VictorC

Right, now I understand... I'm not sure if this can be done for a DataGrid [it can be done for a DataGridView though]

Take a look at this solution:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1317537&SiteID=1




Re: Visual C# General numbering rows!!

Figo Fei - MSFT

Hi,

You can achieve this in Cellpainting event of the DataGridView.

For example:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex > -1)
{
StringFormat sf = new StringFormat(StringFormat.GenericDefault);
sf.Alignment = StringAlignment.Center;
e.PaintBackground(e.CellBounds, true);
e.Graphics.DrawString((e.RowIndex + 1).ToString(), this.Font,
new SolidBrush(Color.Black), e.CellBounds,sf);
e.Handled = true;
}
}

Thanks