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 窗体值传递
{
public partial class FrmShow : Form
{
List<Stu> listStu = null;
FrmMain fMain = null;
DataTable dt = null;
int rowIndex = 0;
public FrmShow(FrmMain fMain)
{
this.fMain = fMain;
InitializeComponent();
listStu = new List<Stu>();
listStu.Add(new Stu("貂蝉", "女", "20"));
listStu.Add(new Stu("张飞", "男", "22"));
listStu.Add(new Stu("曹操", "男", "23"));
listStu.Add(new Stu("吕布", "男", "12"));
listStu.Add(new Stu("荀彧", "男", "23"));
}
private void FrmShow_Load(object sender, EventArgs e)
{
this.dgvShow.DataSource = listStu;
}
private void btnTest_Click(object sender, EventArgs e)
{
Stu s = null;
this.dt = new DataTable();
this.dt.Columns.Add("sName", Type.GetType("System.String"));
this.dt.Columns.Add("sSex", Type.GetType("System.String"));
this.dt.Columns.Add("sAge", Type.GetType("System.String"));
//int j = 0;
for (int i = 0; i < this.dgvShow.RowCount; i++)
{
bool re_value = (bool)dgvShow.Rows[i].Cells[0].EditedFormattedValue;
//判断勾选的
if (re_value)
{
string sName = this.dgvShow.Rows[i].Cells[1].Value.ToString();
string sSex = this.dgvShow.Rows[i].Cells[2].Value.ToString();
string sAge = this.dgvShow.Rows[i].Cells[3].Value.ToString();
this.dt.Rows.Add(new object[] { sName, sSex, sAge });
}
}
if (this.dt != null)
{
//传递到主窗体,更新数据
this.fMain.Chang(this.dt);
//foreach (DataRow item in dt.Rows)
//{
// //this.fMain.AddRow(item);
// MessageBox.Show(item["sName"].ToString()+","+item["sSex"].ToString()+","+item["sAge"].ToString());
//}
this.Close();
}
}
private void tsmi_Del_Click(object sender, EventArgs e)//右击菜单列表的删除
{
this.rowIndex = this.dgvShow.CurrentRow.Index;
MessageBox.Show("Test:" + this.rowIndex.ToString());
/*
* 以下执行删除算法
*
* */
//使用右击菜单列表的删除删除dgvShow选中的行(dgvShow没有绑定数据库是通过List<Stu>加载数据的)
if (dgvShow != null && dgvShow.CurrentCell != null && dgvShow.CurrentCell.RowIndex != -1)
{
dgvShow.Rows.RemoveAt(dgvShow.CurrentCell.RowIndex);
} //报错
//dgvShow.Rows.RemoveAt(rowIndex);
}
private void dgvShow_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.rowIndex = e.RowIndex;
bool re_value = (bool)dgvShow.Rows[this.rowIndex].Cells[0].EditedFormattedValue;
if (!re_value)
{
this.dgvShow.Rows[this.rowIndex].Cells[0].Value = true;
}
else
{
this.dgvShow.Rows[this.rowIndex].Cells[0].Value = false;
}
}
private void dgvShow_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
dgvShow.ClearSelection();
dgvShow.Rows[e.RowIndex].Selected = true;
//MessageBox.Show("Test:" + e.RowIndex.ToString());
//string i = dgvShow.SelectedCells[0].Value.ToString();
//MessageBox.Show(i);
dgvShow.CurrentCell = dgvShow.Rows[e.RowIndex].Cells[e.ColumnIndex];
contextMenuStrip.Show(MousePosition.X, MousePosition.Y);
}
}
}
}
}
相关分类