CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    3

    DataGridView Column Re-Ordering

    Hi,

    I have a datagridview control and a panel control on the form. My 1st target was to drag and drop the column header to the panel so that panel will create the grouping criteria for the datagridview and will sort and group the datagrid accordingly. Till this point I could do everything properly.

    Next task is to allow the users to reorder the columns. Here, I am not able to reorder the columns. As I am handling Drag-Drop manually, it is not allowing to reorder the columns (I have set the AllowUserToOrderColumns property to TRUE). If I comment out DoDragDrop call from the MouseDown event of the DataGrid, I can reorder the columns but I can not drag-drop the column headers. So, I believe it's must to use DoDragDrop method and handle the column ordering manually.

    Any thoughts as to how to proceed in this case?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: DataGridView Column Re-Ordering

    Can we see some code?

  3. #3
    Join Date
    Jul 2005
    Posts
    3

    Re: DataGridView Column Re-Ordering

    Here are my 3 functions. Some of the code in groupbox1_DragDrop is quite redundant. Its just for trial and error thing.

    private void dgv_MouseDown(object sender, MouseEventArgs e)
    {
    colDisplayIndexChanged = true;
    if (dgv.AllowDrop)
    {
    if (dgv.HitTest(e.X, e.Y).ColumnIndex > -1 && dgv.HitTest(e.X, e.Y).RowIndex == -1)
    {
    DoDragDrop(dgv.Columns[dgv.HitTest(e.X, e.Y).ColumnIndex].HeaderText, DragDropEffects.Copy);

    }
    }
    }

    private void groupBox1_DragEnter(object sender, DragEventArgs e)
    {
    if (e.Data.GetDataPresent(DataFormats.Text))
    e.Effect = DragDropEffects.Copy;
    else
    e.Effect = DragDropEffects.None;
    }

    private void groupBox1_DragDrop(object sender, DragEventArgs e)
    {
    repeatedColumn = false;
    repeatedSort = false;
    int width, height, count, prevBtn;
    Point clientPoint, btnLocation;
    string btnName, lastBtn;

    clientPoint = groupBox1.Location;
    clientPoint.X += 2;
    clientPoint.Y += 2;

    count = 0; width = 0; height = 0; prevBtn = 0;

    btnName = ""; lastBtn = "";


    foreach (Control ctrl in groupBox1.Controls)
    {
    if (ctrl is Button)
    count++;
    }

    foreach (Control ctrl in groupBox1.Controls)
    {
    if (ctrl is Button && ctrl.Text == e.Data.GetData(DataFormats.Text).ToString())
    {
    repeatedColumn = true;
    break;
    }
    }

    if (!repeatedColumn)
    {
    Button btn = new Button();
    btn.Text = e.Data.GetData(DataFormats.Text).ToString();
    btnName = "Button" + count.ToString();
    btn.Name = btnName;
    btn.AutoSize = true;

    btnLocation = groupBox1.Location;
    if (count > 0)
    {
    prevBtn = count - 1;
    lastBtn = "Button" + prevBtn.ToString();

    btnLocation.X = groupBox1.Controls[lastBtn].Right + 2;

    width = btnLocation.X + btn.Width;
    if (width <= groupBox1.Width)
    btnLocation.Y = groupBox1.Controls[lastBtn].Top;
    else
    {
    btnLocation = groupBox1.Location;
    btnLocation.X += 2;
    btnLocation.Y += groupBox1.Controls["Button0"].Bottom + 2;
    }

    btn.Location = btnLocation;
    }

    else
    btn.Location = clientPoint;



    groupBox1.Controls.Add(btn);
    btn.BringToFront();

    label2.Text += e.Data.GetData(DataFormats.Text).ToString();
    label1.Text += btn.Name;
    outlookGrid1.flagGroup = true;
    }
    else
    {
    MessageBox.Show("The Criteria is already present!");
    repeatedSort = true;
    }
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured