简体   繁体   English

将ContextMenuStrips添加到多个TabPages上的多个DataGridViews

[英]Add ContextMenuStrips to multiple DataGridViews on multiple TabPages

I am trying to load multiple DataTables into DataGridViews in separate tabpages of a TabControl, the DataTables are stored in a Dictionary which is in turn stored in a DataObject class. 我试图在TabControl的单独选项卡页中将多个DataTables加载到DataGridViews中,DataTables存储在Dictionary中,而Dictionary又存储在DataObject类中。

I can get everything displaying correctly but when I try to add loop through the Datagridview Header to add a contextmenustrip, for some reason I can get it to work for the first DataGridView but subsequent DataGridViews do not have the context menu applied? 我可以正确显示所有内容,但是当我尝试通过Datagridview Header添加循环以添加contextmenustrip时,由于某种原因,我可以使它适用于第一个DataGridView,但是后续的DataGridViews没有应用上下文菜单吗? I have tried adding a print statentment just before the foreach loop and the DataGridView gets a column count of zero…so im guessing that's why the foreach loop isn't doing anything…but all the data is still displayed correctly in the dataGridViews in their respective tabs…any help in pointing out what I'm missing would be greatly appreciated. 我试过在foreach循环之前添加打印声明,并且DataGridView的列数为零…所以我想这就是为什么foreach循环不执行任何操作的原因……但是所有数据仍然正确地显示在各自的dataGridViews中标签…在指出我所缺少的内容方面的任何帮助将不胜感激。

Regards Amarino 问候阿马里诺

Code given below 下面给出的代码

List<ImportObject> lImportObjects = new List<ImportObject>();

private void loadImportFilesToScreen(List<ImportObject> lImportObjects)
{
    foreach (ImportObject lImportObject in lImportObjects) {
        DisplayImportFiles(lImportObject);
    }
}

public void DisplayImportFiles(ImportObject pImportObject)
{
    string lTabName="";
    //load DataGridView with DataTable
    /*
    foreach (KeyValuePair<string, DataTable> lDT in pImportObject.DataTableDictionary)
    {
        lTabName = DisplayTabsInApp(pImportObject.FileName + "_" + lDT.Key, lDT.Key);
        LoadDatatableIntoGrid(lDT.Value, lTabName);
    }
    */
    for (int i = 0; i < pImportObject.DataTableDictionary.Count; i++)
    {
        KeyValuePair<string, DataTable> lItem = pImportObject.DataTableDictionary.ElementAt(i);
        string lKey = lItem.Key;
        DataTable lDT = lItem.Value;
        lTabName = DisplayTabs(pImportObject.FileName + "_" + lKey, lKey);
        LoadDatatableIntoGrid(lDT, lTabName);
        lDT = null;
    }
}

public string DisplayTabs(string pTabName, string pSheetName)
{
    // Create a new Tab Page for this file. Set heading, set name.
    TabPage lTabPage_NewFile = new TabPage();
    lTabPage_NewFile.Text = pTabName;
    lTabPage_NewFile.Name = "TAB_PAGE_" + pTabName;
    tabControl_ImportFiles.TabPages.Add(lTabPage_NewFile);
    return lTabPage_NewFile.Name;
}

public void LoadDatatableIntoGrid(DataTable pDataTable, string pTabName) {
    DataGridView lDGV = new DataGridView();
    lDGV.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;
    lDGV.RowHeadersVisible = false;

    BindingSource BindingSource1 = new BindingSource(); //create new data binding source
    BindingSource1.DataSource = pDataTable;             //SetData source
    lDGV.DataSource = BindingSource1;
    lDGV.RowHeadersVisible = true;

    tabControl_ImportFiles.TabPages[pTabName].Controls.Add(lDGV);
    //DataGridView lDGV = tabControl_ImportFiles.TabPages[pTabName].Controls[0] as DataGridView;
    PrintToConsoleInARD("DataGridView Column Count: " + lDGV.Columns.Count.ToString()); 
    bool runOnce = true; 
    foreach (DataGridViewColumn lDGVColumn in lDGV.Columns) {
        lDGVColumn.HeaderCell.ContextMenuStrip = lCMS_ColumnHeaders;
    }
    lDGV.Dock = DockStyle.Fill;
    lDGV.VirtualMode = true;
    BindingSource1 = null;
    lDGV = null;
}

I am confident the reason for this is because the “tab page” is not displayed. 我深信这是因为未显示“标签页”。 As the trace is described inside the LoadDatatableIntoGrid method… if you put a breakpoint at the line… 由于跟踪是在LoadDatatableIntoGrid方法中描述的……如果在该行上放置断点……

tabControl_ImportFiles.TabPages[pTabName].Controls.Add(lDGV);

Add a watch to the variable lDGV.Columns.Count … will show that IDGV has zero (0) columns. 将监视添加到变量lDGV.Columns.Count …将显示IDGV具有零(0)列。 Execute the line above and magically, the grid IDGV has columns. 执行上面的行,神奇的是,网格IDGV包含列。 This appears correct since this is the first “tab page” and it is the active (displayed) tab page. 这似乎是正确的,因为这是第一个“标签页”,并且它是活动的(显示的)标签页。 Point being… if you do not add the grid to a “Active/Shown/Displayed” tab page most UI code will be ignored. 要点...如果不将网格添加到“活动/显示/显示”选项卡页面,则大多数UI代码将被忽略。 This is why the next time around; 这就是为什么下一次到来; the grid will be empty because the grid is added to a non “active/shown/displayed” tab page. 网格将为空,因为网格已添加到非“活动/显示/显示”标签页中。

A simple solution is to simply “Show” the tab page before you add the grid to it. 一个简单的解决方案是在向其添加网格之前简单地“显示”选项卡页面。 This appears to fix the issue you describe. 这似乎可以解决您描述的问题。 Add the line below before the line above… 在下面的行之前添加下面的行...

tabControl_ImportFiles.TabPages[pTabName].Show();

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM