在datagridview中点击单元格弹出窗体进行选择 返回给原窗体


涉及到的知识:

1.CellClick事件 (注意区别CellContentClick)

CellContentClick 是必须要点中单元格里的内容才触发

CellClick 是只要点中单元格就触发


2.给datagridview 赋值

主要是先给一个datagridview数据源datatable 然后相应修改datatable的值即可


3.弹出新窗体


4.窗体传值(在Form初始化时传值)


5.委托  其实也是用于传值 http://blog.csdn.net/zzq900503/article/details/8980548


6.checkbox的用法


代码如下:

Form1.cs中

cellClick事件

      private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int CIndex = e.ColumnIndex;
                if (CIndex == 9)
                {

                   Form a= new Form3(e.RowIndex,e.ColumnIndex);
                   a.Show();
                  
                }
                else
                {
                    MessageBox.Show("请选择正确的单元格");
                }
            }
            catch
            {
                MessageBox.Show("请点击正确单元格以及检查你的内容是否正确");
            }


        }


委托事件

   public Form1()
        {
            InitializeComponent(); 

            Form3.setStatusDelegate_request = new setStatusDelegate_request((i) =>
            {
                this.Invoke(new setStatusDelegate_request((a) =>
                {
                    string[] returnstr = a.Split('|');
                    int row = int.Parse(returnstr[1].ToString());
                    int col = int.Parse(returnstr[2].ToString());
                    string request=returnstr[0].ToString();
                    string[] b = request.Split(',');
                    string a_id = "";
                    for (int ii = 0; ii < b.Length; ii++)
                    {
                        if (ii == b.Length - 1)
                        {
                            a_id += _ht_request[b[ii]].ToString();
                        }
                        else
                        {
                            a_id += _ht_request[b[ii]].ToString() + ",";
                        }
                    }
                    _dt_Data.Rows[row][col] = request;
                    _dt_Data.Rows[row][17] = a_id;
                }), i);
            });  

        }


Form3.cs

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 Import_Advtisement
{
    public delegate void setStatusDelegate_request(string iteration); 
    public partial class Form3 : Form
    {
        public string rowstr = "1";
        public string colstr = "1";
        public Form3(int row,int col)
        {
            InitializeComponent();

            rowstr = row.ToString();
            colstr = col.ToString();
            this.skinEngine1 = new Sunisoft.IrisSkin.SkinEngine(((System.ComponentModel.Component)(this)));

            this.skinEngine1.SkinFile = Application.StartupPath + "//DiamondBlue.ssk";
            Sunisoft.IrisSkin.SkinEngine se = null;
            se = new Sunisoft.IrisSkin.SkinEngine();
            se.SkinAllForm = true;
        }
        public static setStatusDelegate_request setStatusDelegate_request; 
        private void button1_Click(object sender, EventArgs e)
        {
            string a = "";
            foreach (Control o in this.Controls)
            {
                if (o is CheckBox)
                {
                    if (((CheckBox)o).Checked == true)
                    {
                        a += o.Text.ToString()+",";
                    }
                }
            }
            a = a.Remove(a.LastIndexOf(","), 1);

          a=  a + "|" + rowstr + "|" + colstr;
            if (setStatusDelegate_request != null)
            {
                setStatusDelegate_request(a);
            }

            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}



你可能感兴趣的:(checkbox,datagridview)