Winforms 使用VS2010,C#代码在Windows窗体中合并编辑和删除一列中的列
我在Windows DataGridView中有一个编辑列和另一个删除列,我正在通过编程绑定该列。。假设我有一列“编辑”和另一列“删除”,它们都绑定到“ID”。。现在我想将这两列合并为“编辑/删除”,其中有“编辑”和“删除”的链接。。但是根据VS Studio 2010的C#windows窗体属性的TableLayoutPanel属性,我不能在一个单元格中有两个控件。。如何使用C#Windows窗体代码实现此场景 代码:-Winforms 使用VS2010,C#代码在Windows窗体中合并编辑和删除一列中的列,winforms,c#-4.0,Winforms,C# 4.0,我在Windows DataGridView中有一个编辑列和另一个删除列,我正在通过编程绑定该列。。假设我有一列“编辑”和另一列“删除”,它们都绑定到“ID”。。现在我想将这两列合并为“编辑/删除”,其中有“编辑”和“删除”的链接。。但是根据VS Studio 2010的C#windows窗体属性的TableLayoutPanel属性,我不能在一个单元格中有两个控件。。如何使用C#Windows窗体代码实现此场景 代码:- public frmTopicSectionManagement()
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);
}
}
假设您希望在名为“编辑/删除”的单个单元格中有两个链接,那么我认为您应该使用包含两个链接标签的自定义单元格类型创建一个自定义列类型
更多关于单个datagrid视图单元格中的多个控件的信息[含源代码]
[解决方案1-合并标题]
您不需要合并列,而是像合并标题一样合并标题。然后,请参考中的解决方案。这应该能解决你的问题
[解决方案2-合并列]
您可以在单个数据网格视图单元中承载多个控件,这正是您想要实现的。创建一个带有两个链接标签的winforms用户控件,并将该用户控件托管在datagridview单元格中。因此,您将在单个单元格[您所指的合并列]中进行编辑/删除。请参见此处,了解如何在datagrid视图单元格中托管自定义控件-未了解此内容-“,但根据VS Studio 2010的TableLayoutPanel属性C#windows窗体属性,我可以在一个单元格中拥有两个控件”,我的理解是正确的[您提到的问题陈述]?通过您的屏幕截图,我可以看出-要么是一个单标题,要么是一个包含多个控件的单元格,即两个链接标签[您对“合并列”的命名法感到困惑]。我们已经讨论过这一点&我已经在答案中提供了这两种方法的源代码。你现在必须使用这些方法并制定出符合你要求的代码。-1:那么如果你知道所有事情,你就不应该问这个问题。这意味着您需要一个现成的代码/解决方案来解决您的问题,您可以直接按Ctrl C+Ctrl V组合键