C#如何在TableLayoutPanel中搜索DataGridView [英] C# How to search for a DataGridView inside a TableLayoutPanel

查看:130
本文介绍了C#如何在TableLayoutPanel中搜索DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:

我在TableLayoutPanel中有一个包含许多DataGridViews的项目。你如何在TableLayoutPanel中搜索一个DataGridView来搜索





我可以用这种方式搜索DataGridView:



Hi:
I have a project with many DataGridViews inside a TableLayoutPanel. How do you search
for a DataGridView that exists inside a TableLayoutPanel.

I can search for a DataGridView this way:

private DataGridView FindDataGridViewByName(string name)
{

foreach (DataGridView gridView in this.weeklyTableLayoutPanel.
{
                if (gridView.Name == name)
                    return gridView; //found
}
 return null; //not found
}



但我收到错误因为DataGridView在TableL中ayoutPanel。

我可以通过这种方式搜索控件:


But I get an error because the DataGridView is inside a TableLayoutPanel.
I can search for a control this way:

private Control FindDataGridViewByName(string name)
        {
            

            foreach (Control c in this.weeklyTableLayoutPanel.Controls)
            {
                if (c.Name == name)
                {
                    return c;
                }

             }     

Return null;
}





但是,这不会给我一个DataGridView。



感谢您的时间和专业知识。



此致,



Bosco



But, that doesn’t give me a DataGridView.

Thank you for your time and expertise.

Sincerely,

Bosco

推荐答案

尝试:

Try:
private DataGridView FindDataGridViewByName(string name)
    {
    foreach (Control c in weeklyTableLayoutPanel.Controls)
        {
        if (c.Name == name)
            {
            return c as DataGridView;
            }
        }
    return null;
    }


不要按名称搜索,我的意思是你可以,看看解决方案#1。

除此之外,执行以下操作:

Don't search it by name, I mean you can, look at the solution #1.
And apart from that, do something like :
private Control FindDataGridView(string name)
{
    Control ctrl = new Control();
    foreach (Control c in this.weeklyTableLayoutPanel.Controls)
    {
        if (c.Name == name)
        {
            ctrl  = c;
            break;
        }
    }     
        return ctrl;
}



通过执行以下操作来引用此控件:


And refer this controls by doing something like,

DataGridview g = (DataGrdView)FindDataGridView("Employee");  // I'm not sure about whether it is GridView or DataGridview in Form App





-KR



-KR


如果你使用'Find方法[ ^ ]的ControlCollection对象。



假设您要查找的DataGridView具有唯一的'Name属性',DGV1,'并且位于名为'tableLayoutPanel1:
This is much easier if you use the 'Find method [^] of the ControlCollection object.

Assuming the DataGridView you want to find has a unique 'Name property, 'DGV1,' and is in a TableLayoutPanel named 'tableLayoutPanel1:
// search the top-level Controls of the TableLayoutPanel
DataGridView foundDGV1 = tableLayoutPanel1.Controls.Find("DGV1",false)[0] as DataGridView;

// search all Controls on the Form, and search within other ContainerControls (recursive)
DataGridView foundDGV2 = this.Controls.Find("DGV1", true)[0] as DataGridView;

如果你不确定你使用'Find包含你搜索的项包含的ContainerControl的ControlCollection,那么就是另一个技术指示:

If you are not sure that the ContainerControl's ControlCollection you search using 'Find contains the item you search for, then a different technique is indicated:

private DataGridView GetDataGridView(string targetName, Control sourceContainerControl, bool doRecursiveSearch)
{
    var dgv = sourceContainerControl.Controls.Find(targetName, doRecursiveSearch);

    return (dgv.Length == 0) ? null : dgv[0] as DataGridView;
}

如果我执行此代码:

var foundDGV1 = GetDataGridView("BADNAME1", tableLayoutPanel1, false);

if(foundDGV1 != null)
{
    // found: do something with valid DataGridView search result
}
else
{
    // not found ... ?
}


这篇关于C#如何在TableLayoutPanel中搜索DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆