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的更多相关文章

  1. Winform子窗体刷新父窗体

    调用窗体(父):Form1,被调用窗体(子):Form2方法1:   所有权法//Form1://需要有一个公共的刷新方法public   void   Refresh_Method(){//...} ...

  2. Winfrom子窗体刷新父窗体

    本人比较懒,直接从网上转载了一篇比较合适的文章,只是文章格式有点乱,地址是 http://aspnet.blog.163.com/blog/static/17515510920121126104433 ...

  3. winform打开子窗体后,在子窗体中刷新父窗体,或者关闭子窗体刷新父窗体

    winform打开子窗体后,在子窗体中刷新父窗体,或者关闭子窗体刷新父窗体,搜集了几个方法,列举如下: 一 . 所有权法 父窗体,名称为“fuForm”,在父窗体中有个公共刷新方法,也就是窗体数据初始 ...

  4. Winform 子窗体设置刷新父窗体

    方法1:所有权法 父窗体:Form1    子窗体:Form2 //Form1:窗体代码 //需要有一个公共的刷新方法 public void Refresh_Method() { //... } / ...

  5. js后台提交成功后 关闭当前页 并刷新父窗体(转)

    原文地址:http://www.cnblogs.com/chenghu/p/3696433.html 后台提交成功后 关闭当前页 并刷新父窗体 this.ClientScript.RegisterSt ...

  6. javascript关闭弹出窗体时刷新父窗体和居中显示弹出窗

    居中显示用到了moveTO()方法: 关闭弹出窗时刷新父窗体用到了window.opener方法: 父窗体代码例如以下: <%@ Page Language="C#" Aut ...

  7. silverlight子窗体操作数据库后刷新父窗体

    silverlight子窗体操作数据库后刷新父窗体 作者 Kant 写于 2011 年 07 月 02 日 分类目录 学习笔记, 所有文章 C# Silverlight 代码 刷新 学习 异步刷新 数 ...

  8. js后台提交成功后 关闭当前页 并刷新父窗体

    后台提交成功后 关闭当前页 并刷新父窗体 this.ClientScript.RegisterStartupScript(this.GetType(), "message", &q ...

  9. winform c#中子窗体关闭刷新父窗体

    父窗体Form1 子窗体Form2 Form1中有一个datagridview控件和一添加按钮,Form2中有一个Text控件和一个保存按钮 要求点击Form1窗体上的添加按钮,弹出Form2,再te ...

随机推荐

  1. EntityFramework优化:第一次启动优化

    1. 预先生成视图 通过代码的方式来预先生成视图,要求EntityFramework是6.0及以上版本. 控制台程序: using System.Data.Entity.Infrastructure; ...

  2. red()、redinle()、redlines()三者之间的关系

    # 关于read()方法: # 1.读取整个文件,将文件内容放到一个字符串变量中 # 2.如果文件大于可用内存,不可能使用这种处理 file_object = open("a.txt&quo ...

  3. Django的View(视图)和路由系统

    一.Django的View(视图) 1.介绍 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一 ...

  4. Python之常见算法介绍

    一.算法介绍 1. 算法是什么 算法是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制.也就是说,能够对一定规范的输入,在有限时间内获得所要求的输 ...

  5. WinForm登录验证

    概述:输错三次禁止登陆,15分钟后才能继续. 图示: Form1代码: using System; using System.Configuration; using System.Data.SqlC ...

  6. ajax实现长连接

    项目需求:需要实时的读取日志文件里的数据,并且使用Echart实时更新折线图. 使用ajax实现客户端与服务器端的数据传输. 目的:我想通过ajax与服务器建立一个长连接,服务器会不断的传输数据给前台 ...

  7. jmeter将上一个接口返回值作为下一个接口的请求参数

    在jmeter中有时候会用到,将上一个接口的返回值作为下一个接口的请求参数 具体操作如下: 1.首先新建一个http请求(右键线程组--添加Sampler--http请求),同时添加好接口相应的请求参 ...

  8. 美化博客CSS

    title: 美化博客CSS date: 2019/01/19 14:28:59 --- 美化博客CSS 可以去这里看下好看的样式 修改下文档的css,博客园是在页面定制CSS代码,我这里修改了下标题 ...

  9. 18、cookies与session学习笔记

    本文记录学习 cookies 和 session 的一些小练习和知识点   知识点1    cookies 和 session 的由来   HTTP协议是无状态的协议,因为一旦浏览器和服务器之间的请求 ...

  10. JavaScript 基本类型和引用类型

    前言 ECMAScript变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象. 基本类型 Undefined.Null.B ...