c# - 如何使用 C# Winforms 从子窗体调用父窗体中的数据表?
问题描述
我尝试使用 ParentForm 类或 Parent 类创建一个实例,但无法获取已在父窗体中创建的数据表。请在我试图从我的子表单中获取的父表单中找到以下代码示例。
private void ParentForm_Load(object sender, EventArgs e)
{
try
{
BLGridsToControls objBL = new BLGridsToControls();
dgMainGrid.DataSource = objBL.GetCity();
}
catch
{
throw;
}
注意:objBL.GetCity 返回一个数据表
解决方案
您可以通过创建一个接受 DataTable 作为参数的构造函数,然后调用该构造函数,将 dataTable 传递给子窗体。例如
var childForm = new ChildForm(dgMainGrid.DataSource);
childForm.ShowDialog();
在 ChildForm 中:
DataTable myDataSource { get; set; }
ChildForm(DataTable myDataTable)
{
// do something with myDataTable, such as put it into a property for later use,
myDataSource = myDataTable;
}
推荐阅读
- java - HazelCast - 过期监听器延迟
- asp.net-mvc - Oracle Entity 只创建一张表
- json - 输出分号分隔的字符串
- security - Kafka 到 Zookeeper DIGEST-MD5 身份验证错误
- python - 并行化数据帧拆分和处理
- html - 导航子菜单 - 如果窗口太窄,则显示在左侧
- cakebuild - 从辅助脚本文件引用插件
- ios - 编译 Xcode 9 项目时出错:架构 arm64 的未定义符号:“_mptcp_go”
- spring-boot - EmbeddedKafka AdminClient 在 Spring 应用程序启动测试之前关闭
- jquery - 从 document.location.href 获取完整的 URL 查询字符串?