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