Forum  Core :: SDK (Vi...  Using the Visua...  Labeling lines in DataGridView
Previous Previous
 
Next Next
New Post 5/24/2011 10:39 AM
  s.silay@login.com.tr
17 posts
No Ranking


Labeling lines in DataGridView 

 Is there a way to label lines in DataGridView?

 

The label shouldnt be a data, instead the label numbers should update when lines are sorted. For example;

 

1 Sun

2 Microsoft

3 Apple

when sorted Alphabetically

 

1 Apple

2 Microsoft

3 Sun

 

Thanks 

Sahane

 
New Post 5/24/2011 1:29 PM
  palli
14324 posts
1st Level Poster




Re: Labeling lines in DataGridView 

Hi Sahene,

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);
 
        }
    }

Hope this helps and/or gives you some ideas for an implementation,

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
Previous Previous
 
Next Next
  Forum  Core :: SDK (Vi...  Using the Visua...  Labeling lines in DataGridView
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS