将新数据添加到Datagridview时创建标签

时间:2016-02-23 09:21:07

标签: c# winforms datagridview panel flowlayoutpanel

将新数据添加到label时,是否可以在单独的flowlayoutpanel上创建datagridview?我所做的就是在设计器中创建static labels,我可以将它们从flowlayoutpanel移到flowlayoutpanel

private void control_MouseDown(object sender, MouseEventArgs e)
{
    var control = sender as Control;
    this.DoDragDrop(control.Name, DragDropEffects.Move);
}

private void panel_DragEnter(object sender, DragEventArgs e)
{
    if (!e.Data.GetDataPresent(typeof(string)))
        return;

    var name = e.Data.GetData(typeof(string)) as string;
    var control = this.Controls.Find(name, true).FirstOrDefault();
    if (control != null)
    {
        e.Effect = DragDropEffects.Move;
    }
}

private void panel_DragDrop(object sender, DragEventArgs e)
{
    if (!e.Data.GetDataPresent(typeof(string)))
        return;

    var name = e.Data.GetData(typeof(string)) as string;
    var control = this.Controls.Find(name, true).FirstOrDefault();
    if (control != null)
    {
        control.Parent.Controls.Remove(control);
        var panel = sender as FlowLayoutPanel;
        ((FlowLayoutPanel)sender).Controls.Add(control);
    }
}

1 个答案:

答案 0 :(得分:0)

我这样解决了这个问题。

 foreach (DataGridViewRow row in dgv.SelectedRows)
        {
            var Id = row.Cells["ID"].Value.ToString();
            int leadID;

            if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out ID))
            {
                Label label = new Label();

                label.Text = row.Cells["Name"].Value.ToString();

            }
        }