Under normal circumstances, using the out of the box DataGridView, you would use the row.HeaderCell.Value and assign it the counter, and then you would have to loop through the rows every time sorting is changed, and update the labels.
There is a catch however, as the row.HeaderCell is not implemented in Visual WebGui at the moment, but will be when this tracker entry here is completed.
You do of course have the option of diving down to the JavaScript, XSLT and CSS resources for the DataGridView and create a custom DataGridView, but as the DataGridView is probably the most complex control of the framework, you will need considerable amount of time to complete that customization.
Using the same basic idea as you would when the row.HeaderCell is implemented, you may simply want to add a single unbound column to the DataGridView and add your row labels there. Using that method (which will be very similar using the HeaderCell), you could create an inherited DataGridView control and override the Sort methods for adding your labels every time the DataGridView is sorted. The code below does just that, as well as implementing a new event that will fire to the hosting container when labels need to be updated.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private mydgv dataGridView1;
private void Form2_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Columns.Add("Col3");
for (int i = 0; i < 30; i++)
for (int j = 0; j < 3; j++)
dt.Rows.Add(i * 3, (i * 3) + 1, (i * 3) + 2);
dataGridView1 = new mydgv();
dataGridView1.BorderStyle = Gizmox.WebGUI.Forms.BorderStyle.FixedSingle;
dataGridView1.Location = new System.Drawing.Point(25, 98);
dataGridView1.Size = new System.Drawing.Size(641, 318);
this.Controls.Add(dataGridView1);
dataGridView1.AutoGenerateColumns = false;
DataGridViewTextBoxColumn tc = new DataGridViewTextBoxColumn();
tc.DataPropertyName = "";
tc.Width = 30;
tc.ReadOnly = true;
dataGridView1.Columns.Add(tc);
tc = new DataGridViewTextBoxColumn();
tc.DataPropertyName = "Col1";
dataGridView1.Columns.Add(tc);
tc = new DataGridViewTextBoxColumn();
tc.DataPropertyName = "Col2";
dataGridView1.Columns.Add(tc);
tc = new DataGridViewTextBoxColumn();
tc.DataPropertyName = "Col3";
dataGridView1.Columns.Add(tc);
dataGridView1.DataSource = dt;
dataGridView1.AddRownumbers += AddRownumbers;
this.AddRownumbers(this.dataGridView1, EventArgs.Empty );
}
private void AddRownumbers(Object sender, EventArgs e)
{
mydgv DGV = (mydgv)sender;
textBox1.Text += "Added rownumbers" + Environment.NewLine;
foreach (DataGridViewRow r in DGV.Rows)
{
if (!r.IsNewRow)
{
// r.HeaderCell.Value = r.Index.ToString();
r.Cells[0].Value = r.Index.ToString();
}
}
}
}
public class mydgv : DataGridView
{
public event EventHandler AddRownumbers;
public override void Sort(System.Collections.IComparer objComparer)
{
base.Sort(objComparer);
if (this.AddRownumbers != null )
this.AddRownumbers (this, EventArgs.Empty);
}
public override void Sort(DataGridViewColumn objDataGridViewColumn, ListSortDirection enmDirection)
{
base.Sort(objDataGridViewColumn, enmDirection);
if (this.AddRownumbers != null)
this.AddRownumbers(this, EventArgs.Empty);
}
}