【winform】userControl刷新父窗体的datagridview
1.ContextMenuStrip 获取右键控件名称
this.contextMenuScriptScore.SourceControl.Name; //当前控件名
2.radiobutton 分组
放入一个Panel里面。
例如,放入一直flowLayoutPanel里
3.动态生成一个Label,放入flowLayoutPanel里。
private void AddLable(int number, string text )
{
Label lable = new Label();
lable.Name = "lab_tgt_word_" + number;
lable.Text = text;
lable.AutoSize = true;
lable.BorderStyle = BorderStyle.FixedSingle;
lable.Size = new Size(50, 50);
lable.Margin = new Padding(0, 0, 10, 20);
lable.Location = new Point(this.flowLayoutPanel2.Size);
lable.ContextMenuStrip = contextMenuScriptScore; //右键菜单
this.flowLayoutPanel2.Controls.Add(lable);
}
4.用户控件(userControl)刷新父窗体的datagridview
//2.userControl刷新父窗体 datagridview
var mainForm = (Main)this.Parent;
DataTable table = (DataTable)mainForm.dataGridView1.DataSource;
table.Rows[tableJson.ID - 1]["tgt_len_compute"] = tableJson.TgtLenCompute.ToString();
mainForm.dataGridView1.DataSource = table;
5.datagridview导出excel,去掉一列,不改变datagridview
DataGridView dgv = this.dataGridView1;
DataTable dataTable = (DataTable)dgv.DataSource;
//必须copy,才不会改变datagridview的绑定源
DataTable datatableCopy = dataTable.Copy();
datatableCopy.Columns.Remove("ID");
string sheetName = datatableCopy.Rows[0][0].ToString().ToUpper() + "&" + datatableCopy.Rows[0][1].ToString().ToUpper();
ExcelHelper.ExportToExcel(datatableCopy, sheetName, saveFileDialog1.FileName);
ExcelHelper.ExportToExcel(datatableCopy, sheetName, saveFileDialog1.FileName);
6.自定义MessageBox 按钮的文本
使用
DialogResult dialog = MessageBoxEx.Show("does it load directly or does it recalculate?", "hint", MessageBoxButtons.YesNo, new String[] { "load", "recalculate" });
代码
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace Sphinx.Utils
{
public class MessageBoxEx
{
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles)
{
MessageForm frm = new MessageForm(buttons, buttonTitles);
frm.Show();
frm.WatchForActivate = true;
DialogResult result = MessageBox.Show(frm, text, caption, buttons);
frm.Close();
return result;
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,
MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string[] buttonTitles)
{
MessageForm frm = new MessageForm(buttons, buttonTitles);
frm.Show();
frm.WatchForActivate = true;
DialogResult result = MessageBox.Show(frm, text, caption, buttons, icon, defaultButton);
frm.Close();
return result;
}
class MessageForm : Form
{
IntPtr _handle;
MessageBoxButtons _buttons;
string[] _buttonTitles = null;
bool _watchForActivate = false;
public bool WatchForActivate
{
get { return _watchForActivate; }
set { _watchForActivate = value; }
}
public MessageForm(MessageBoxButtons buttons, string[] buttonTitles)
{
_buttons = buttons;
_buttonTitles = buttonTitles;
// Hide self form, and don't show self form in task bar.
this.Text = "";
this.StartPosition = FormStartPosition.CenterScreen;
this.Location = new Point(-32000, -32000);
this.ShowInTaskbar = false;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
// Hide self form, don't show self form even in task list.
NativeWin32API.SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, 659);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (_watchForActivate && m.Msg == 0x0006)
{
_watchForActivate = false;
_handle = m.LParam;
CheckMsgbox();
}
base.WndProc(ref m);
}
private void CheckMsgbox()
{
if (_buttonTitles == null || _buttonTitles.Length == 0)
return;
// Button title index
int buttonTitleIndex = 0;
// Get the handle of control in current window.
IntPtr h = NativeWin32API.GetWindow(_handle, GW_CHILD);
// Set those custom titles to the three buttons(Default title are: Yes, No and Cancle).
while (h != IntPtr.Zero)
{
if (NativeWin32API.GetWindowClassName(h).Equals("Button"))
{
if (_buttonTitles.Length > buttonTitleIndex)
{
// Changes the text of the specified window's title bar (if it has one).
// If the specified window is a control, the text of the control is changed.
// However, SetWindowText cannot change the text of a control in another application.
NativeWin32API.SetWindowText(h, _buttonTitles[buttonTitleIndex]);
buttonTitleIndex++;
}
}
// Get the handle of next control in current window.
h = NativeWin32API.GetWindow(h, GW_HWNDNEXT);
}
}
}
public const int GW_CHILD = 5;
public const int GW_HWNDNEXT = 2;
public class NativeWin32API
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, int flags);
[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hWnd, Int32 wCmd);
[DllImport("user32.dll")]
public static extern bool SetWindowText(IntPtr hWnd, string lpString);
[DllImport("user32.dll")]
public static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
public static string GetWindowClassName(IntPtr handle)
{
StringBuilder sb = new StringBuilder(256);
// Retrieves the name of the class to which the specified window belongs
GetClassNameW(handle, sb, sb.Capacity);
return sb.ToString();
}
}
}
}
【winform】userControl刷新父窗体的datagridview的更多相关文章
- Winform子窗体刷新父窗体
调用窗体(父):Form1,被调用窗体(子):Form2方法1: 所有权法//Form1://需要有一个公共的刷新方法public void Refresh_Method(){//...} ...
- Winfrom子窗体刷新父窗体
本人比较懒,直接从网上转载了一篇比较合适的文章,只是文章格式有点乱,地址是 http://aspnet.blog.163.com/blog/static/17515510920121126104433 ...
- winform打开子窗体后,在子窗体中刷新父窗体,或者关闭子窗体刷新父窗体
winform打开子窗体后,在子窗体中刷新父窗体,或者关闭子窗体刷新父窗体,搜集了几个方法,列举如下: 一 . 所有权法 父窗体,名称为“fuForm”,在父窗体中有个公共刷新方法,也就是窗体数据初始 ...
- Winform 子窗体设置刷新父窗体
方法1:所有权法 父窗体:Form1 子窗体:Form2 //Form1:窗体代码 //需要有一个公共的刷新方法 public void Refresh_Method() { //... } / ...
- js后台提交成功后 关闭当前页 并刷新父窗体(转)
原文地址:http://www.cnblogs.com/chenghu/p/3696433.html 后台提交成功后 关闭当前页 并刷新父窗体 this.ClientScript.RegisterSt ...
- javascript关闭弹出窗体时刷新父窗体和居中显示弹出窗
居中显示用到了moveTO()方法: 关闭弹出窗时刷新父窗体用到了window.opener方法: 父窗体代码例如以下: <%@ Page Language="C#" Aut ...
- silverlight子窗体操作数据库后刷新父窗体
silverlight子窗体操作数据库后刷新父窗体 作者 Kant 写于 2011 年 07 月 02 日 分类目录 学习笔记, 所有文章 C# Silverlight 代码 刷新 学习 异步刷新 数 ...
- js后台提交成功后 关闭当前页 并刷新父窗体
后台提交成功后 关闭当前页 并刷新父窗体 this.ClientScript.RegisterStartupScript(this.GetType(), "message", &q ...
- winform c#中子窗体关闭刷新父窗体
父窗体Form1 子窗体Form2 Form1中有一个datagridview控件和一添加按钮,Form2中有一个Text控件和一个保存按钮 要求点击Form1窗体上的添加按钮,弹出Form2,再te ...
随机推荐
- Linux程序宕掉后如何通过gdb查看出错信息
我们在编写服务端程序的时候,由于多线程并且环境复杂,程序可能在不确定条件的情况下宕掉,还不好重新,这是我们如何获取程序的出错信息,一种方法通过打日志,有时候一些错误日志也不能体现出来,这时就用到我们的 ...
- C# 数组、HashSet等内存耗尽的解决办法
在C#中,如果数据量太大,就会出现 'System.OutOfMemoryException' 异常. 解决办法来自于Stack Overflow和MSDN https://docs.micro ...
- Vue——报错总结
[Vue warn]: Cannot find element: #app [报错原因] 1. 把对应js放在了head标签里面,页面没有加载完成就进行渲染,导致找不到#app. 2.加了<te ...
- HTML- 标签语法
HTML 标签语言 概念 超文本标记语言, 是一种用于创建网页的标记语言 ps: 不是编程语言 利用标签来描述网页 扩展名:.html .htm 语法规范 标签不区分大小写, 推荐小写 双标签必须写 ...
- 【BZOJ5506】[GXOI/GZOI2019]旅行者(最短路)
[BZOJ5506][GXOI/GZOI2019]旅行者(最短路) 题面 BZOJ 洛谷 题解 正着做一遍\(dij\)求出最短路径以及从谁转移过来的,反过来做一遍,如果两个点不由同一个点转移过来就更 ...
- luogu4705玩游戏
题解 我们要对于每个t,求一个(1/mn)sigma(ax+by)^t. 把系数不用管,把其他部分二项式展开一下: simga(ax^r*by^(t-r)*C(t,r)). 把组合数拆开,就变成了一个 ...
- mongodb 3.6 集群搭建:分片+副本集
mongodb是最常用的nosql数据库,在数据库排名中已经上升到了前六.这篇文章介绍如何搭建高可用的mongodb(分片+副本)集群. 在搭建集群之前,需要首先了解几个概念:路由,分片.副本集.配置 ...
- Eclipse 添加 Source 源代码、Javadoc 文档
源代码 Source 按住 Ctrl 键,鼠标放到对应的类.方法上,出现 Open Declaration,Open Implementation ,可查看对应的实现.声明源代码. 也可以在[Proj ...
- openstack项目【day24】:OpenStack mitaka部署
前言: openstack的部署非常简单,简单的前提建立在扎实的理论功底,本人一直觉得,玩技术一定是理论指导实践,网上遍布个种搭建方法都可以实现一个基本的私有云环境,但是诸位可曾发现,很多配置都是重复 ...
- LFYZ-OJ ID: 1011 hanoi双塔问题
思路 虽然每种大小盘子数量为2,但对总步数的影响只是一个简单的倍数关系而已,递推关系很容易可以总结出来:an=an-1+2+an-1=2(an-1+1),n=1时,a1=2.故递推的过程就是从a1=2 ...