本文介绍了帮助代表csharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在实现两种形式的功能。在form1中选择任何datagridviewcheckboxcolumn并在单击按钮关闭时显示form2,在form1 datagridviewcheckboxcolumn中将为false?
请帮忙。
命名空间WindowsFormsApplication1
{
公共部分类Form1:表格
{
Form2 Frm2 = new Form2();
public Form1()
{
InitializeComponent();
DataGridViewCheckColumn();
Frm2.newEvent + = new FrmCheckbox(Frm2_Event);
}
public void DataGridViewCheckColumn()
{
dataGridView1.ColumnCount = 6;
dataGridView1.AllowUserToAddRows = false;
DataGridViewCheckBoxColumn colCB = new
DataGridViewCheckBoxColumn();
colCB.Name =chkcol;
colCB.HeaderText =选择;
colCB.Width = 40;
dataGridView1.Columns.Add(colCB);
dataGridView1.Columns [0] .Name =C0;
dataGridView1.Columns [1] .Name =C1;
dataGridView1.Columns [2] .Name =C2;
dataGridView1.Columns [3] .Name =C3;
dataGridView1.Columns [4] .Name =C4;
dataGridView1.Columns [5] .Name =C5;
dataGridView1.Columns [0] .HeaderText =C0;
dataGridView1.Columns [1] .HeaderText =C1;
dataGridView1.Columns [2] .HeaderText =C2;
dataGridView1.Columns [3] .HeaderText =C3;
dataGridView1.Columns [4] .HeaderText =C4;
dataGridView1.Columns [5] .HeaderText =C5;
dataGridView1.Columns [0] .Width = 40;
dataGridView1.Columns [1] .Width = 40;
dataGridView1.Columns [2] .Width = 40;
dataGridView1.Columns [3] .Width = 40;
dataGridView1.Columns [4] .Width = 40;
dataGridView1.Columns [5] .Width = 40;
dataGridView1.Rows.Add(new string [] {A1,B1,C1,
D1,E1 ,F1});
dataGridView1.Rows.Add(new string [] {A2,B2,C2,
D2,E2,F2});
dataGridView1.Rows.Add(new string [] {A3,B3,C3,
D3,E3,F3});
dataGridView1.Rows.Add(new string [] {A4,B4,C4,
D4,E4,F4});
dataGridView1.Rows.Add(new string [] {A5,B5,C5,
D5,E5,F5});
dataGridView1.Rows.Add(new string [] {A6,B6,C6,
D6,E6,F6});
dataGridView1.Rows.Add(new string [] {A7,B7,C7,
D7,E7,F7});
dataGridView1.AutoResizeColumns();
}
void Frm2_Event()
{
// for(int i = 0; i< = dataGridView1.Rows.Count - 1 ; i ++)
// {
// if(Convert.ToBoolean(dataGridView1.Rows [i] .Cells
[chkcol]。Value) == true)
// {
// dataGridView1.Rows [i] .Cells [chkcol]。Value = false;
//}
//}
foreach(DataGridViewRow中的dataGridView1.Rows行)
{
if(Convert.ToBoolean(row.Cells [ chkcol]。值)== true)
{
row.Cells [chkcol]。值= false;
}
}
}
private void dataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
for(int i = 0; i< = dataGridView1.RowCount - 1; i ++)
{
if(Convert.ToBoolean(dataGridView1.Rows
[ e.RowIndex] .Cells [chkcol]。Value = true))
{
Frm2.ShowDialog();
休息;
}
}
}
}
}
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
命名空间WindowsFormsApplication1
{
public delegate void FrmCheckbox();
公共部分类Form2:表格
{
公共事件FrmCheckbox newEvent;
public Form2()
{
InitializeComponent();
//button1.Click + = new EventHandler(button1_Click);
}
private void button1_Click(object sender,EventArgs e)
{
if(newEvent!= null)
{
newEvent( );
}
}
}
}
解决方案
I'm implementing a function of two forms. While choosing any of datagridviewcheckboxcolumn in form1 and show form2 when click button to close , in the form1 datagridviewcheckboxcolumn will is false ?
Please help on this.
namespace WindowsFormsApplication1 { public partial class Form1 : Form { Form2 Frm2 = new Form2(); public Form1() { InitializeComponent(); DataGridViewCheckColumn(); Frm2.newEvent += new FrmCheckbox(Frm2_Event); } public void DataGridViewCheckColumn() { dataGridView1.ColumnCount = 6; dataGridView1.AllowUserToAddRows = false; DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn(); colCB.Name = "chkcol"; colCB.HeaderText = "Select"; colCB.Width = 40; dataGridView1.Columns.Add(colCB); dataGridView1.Columns[0].Name = "C0"; dataGridView1.Columns[1].Name = "C1"; dataGridView1.Columns[2].Name = "C2"; dataGridView1.Columns[3].Name = "C3"; dataGridView1.Columns[4].Name = "C4"; dataGridView1.Columns[5].Name = "C5"; dataGridView1.Columns[0].HeaderText = "C0"; dataGridView1.Columns[1].HeaderText = "C1"; dataGridView1.Columns[2].HeaderText = "C2"; dataGridView1.Columns[3].HeaderText = "C3"; dataGridView1.Columns[4].HeaderText = "C4"; dataGridView1.Columns[5].HeaderText = "C5"; dataGridView1.Columns[0].Width = 40; dataGridView1.Columns[1].Width = 40; dataGridView1.Columns[2].Width = 40; dataGridView1.Columns[3].Width = 40; dataGridView1.Columns[4].Width = 40; dataGridView1.Columns[5].Width = 40; dataGridView1.Rows.Add(new string[] { "A1", "B1", "C1", "D1", "E1", "F1" }); dataGridView1.Rows.Add(new string[] { "A2", "B2", "C2", "D2", "E2", "F2" }); dataGridView1.Rows.Add(new string[] { "A3", "B3", "C3", "D3", "E3", "F3" }); dataGridView1.Rows.Add(new string[] { "A4", "B4", "C4", "D4", "E4", "F4" }); dataGridView1.Rows.Add(new string[] { "A5", "B5", "C5", "D5", "E5", "F5" }); dataGridView1.Rows.Add(new string[] { "A6", "B6", "C6", "D6", "E6", "F6" }); dataGridView1.Rows.Add(new string[] { "A7", "B7", "C7", "D7", "E7", "F7" }); dataGridView1.AutoResizeColumns(); } void Frm2_Event() { //for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++) //{ // if (Convert.ToBoolean(dataGridView1.Rows[i].Cells ["chkcol"].Value) == true ) // { // dataGridView1.Rows[i].Cells["chkcol"].Value = false ; // } //} foreach (DataGridViewRow row in dataGridView1.Rows) { if (Convert.ToBoolean(row.Cells["chkcol"].Value) == true) { row.Cells["chkcol"].Value = false; } } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { for (int i = 0; i <= dataGridView1.RowCount - 1; i++) { if (Convert.ToBoolean(dataGridView1.Rows [e.RowIndex].Cells["chkcol"].Value = true)) { Frm2.ShowDialog(); break; } } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public delegate void FrmCheckbox(); public partial class Form2 : Form { public event FrmCheckbox newEvent; public Form2() { InitializeComponent(); //button1.Click +=new EventHandler (button1_Click ); } private void button1_Click(object sender, EventArgs e) { if (newEvent != null) { newEvent(); } } } }
解决方案
这篇关于帮助代表csharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!