0

在此处输入图像描述我在 Windows DataGridView 中有一个 Edit 列和另一个 Delete 列,我以编程方式绑定它们。假设我有一个列“Edit”和另一个“Delete”,它们都绑定到“ID”。现在我想合并这些两列都是“编辑/删除”,其中将有“编辑”和“删除”的链接..但是根据 VS Studio 2010 的 C# windows 窗体属性的 TableLayoutPanel 属性,我不能在一个单元格中有两个控件.. 如何使用 C# Windows 窗体代码实现此场景

代码 :-

public frmTopicSectionManagement()

{

InitializeComponent();

PopulateTopicList();

PopulateGridView();

}

private void PopulateGridView()

{

try

{

string PYear = "";

int pId;

int TopicId = Convert.ToInt32(((Topiclist)cmbTopics.SelectedItem).TopicId);

PYear = StartUp.PlanYear.ToString();

pId = StartUp.ProductTypId;

TopicSectionLookup objTopicSection = new TopicSectionLookup();

if (repo.GetAllTopicSections() != null && TopicId == -1 && PYear != "")


  {

 GridView_TopicSection.DataSource = repo.GetAllTopicSectionDisplayTopicTitle(pId, PYear).ToList();                    GridView_TopicSection.Columns["ProductType"].Visible = false;

 GridView_TopicSection.Columns["Year"].Visible = false;                 
PopulateAllGridView();

        }
    else if (repo.GetAllTopicSections() != null)
    {
    GridView_TopicSection.DataSource = repo.GetAllTopicsForSectionManagement(TopicId, pId, PYear).ToList();
                GridView_TopicSection.Columns["ProductType"].Visible = false;
                GridView_TopicSection.Columns["Year"].Visible = false;
                PopulateAllGridView();
            }
            else
            {
                return;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
              }

    private void PopulateAllGridView()
    {
        try
        {
            //Add Update Button to DataGridView(Problems here)
            if (!GridView_TopicSection.Columns.Contains("Update") || !!GridView_TopicSection.Columns.Contains("Delete"))
            {
                DataGridViewLinkColumn updateColumn = new DataGridViewLinkColumn();
                updateColumn.Name = "Edit";
                updateColumn.HeaderText = "Update";
                updateColumn.Text = "Edit";
                updateColumn.Width = 50;
                updateColumn.DividerWidth = 0;
                updateColumn.UseColumnTextForLinkValue = true;
                GridView_TopicSection.Columns.Add(updateColumn);

                //  Add Delete Button to GridView (and here)
                DataGridViewLinkColumn deletecolumn = new DataGridViewLinkColumn();
                deletecolumn.Name = "Delete";
                deletecolumn.HeaderText = "Delete";
                deletecolumn.Text = "Delete";
                deletecolumn.Width = 103;
                deletecolumn.UseColumnTextForLinkValue = true;
                GridView_TopicSection.Columns.Add(deletecolumn);
            }

            GridView_TopicSection.Columns["TopicId"].Visible = false;
            GridView_TopicSection.Columns["TopicSectionLookupId"].Visible = false;
            GridView_TopicSection.Columns["TopicTitle"].Visible = true;
            GridView_TopicSection.Columns["TopicTitle"].Width = 200;
            GridView_TopicSection.Columns["SectionText"].Visible = true;
            GridView_TopicSection.Columns["SectionText"].Width = 200;
            GridView_TopicSection.Columns["Year"].Visible = false;
            GridView_TopicSection.Columns["ProductTypeId"].Visible = false;
            GridView_TopicSection.Columns["IsActive"].Visible = true;
            GridView_TopicSection.Columns["IsActive"].Width = 80;
            GridView_TopicSection.Columns["IsBenifit"].Visible = true;
            GridView_TopicSection.Columns["IsBenifit"].Width = 80;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
4

1 回答 1

0

假设您希望在名为Edit / Delete的单个单元格中有两个链接,那么我认为您应该使用包含两个 LinkLabel 的自定义单元格类型创建一个自定义列类型。

更多关于单个数据网格视图单元格中的多个控件[带有源代码]

[解决方案 1 -合并标题]

您不需要合并列,而是合并标题 - 像这样。然后,请参考相关 SO 帖子中的解决方案。这应该可以解决您的问题。

[解决方案 2 -合并列] 您可以在单个数据网格视图单元格中托管多个控件,这就是您想要实现的。创建一个带有两个链接标签的 winforms 用户控件,并将该用户控件托管在 datagridview 单元格中。因此,您将在单个单元格中进行编辑/删除[您所指的合并列]。请参阅此处以在数据网格视图单元格中托管自定义控件 - http://msdn.microsoft.com/en-us/library/7tas5c80.aspx?PHPSESSID=o1fb21liejulfgrptbmi9dec92

于 2012-05-30T13:19:49.340 回答