20151210001 DataGridView 选中与被选中

时间:2021-12-11 01:07:41

// DataSet_IP list
        private void DataSet_IP_list()
        {
            DataSet_IP = new System.Data.DataSet();
            DataTable tb = new DataTable("Ds_IP");
            DataColumnCollection columns = tb.Columns;
            columns.Add("IP", typeof(String));
            columns.Add("机号", typeof(String));
            DataSet_IP.Tables.Add(tb);
           
            Int32 int_ip = dgv_IP2.Rows.Count;
            for (int i = 0; i < dgv_IP2.Rows.Count; i++)
            {
                if (dgv_IP2.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True")
                {
                    //array_ip = new string[] { dgv_IP2.Rows[i].Cells[1].Value.ToString() };
                    //array_mac = new string[] { dgv_IP2.Rows[i].Cells[2].Value.ToString() };
                    //MessageBox.Show(dgv_IP2.Rows[i].Cells[2].Value.ToString());

string str1 = dgv_IP2.Rows[i].Cells[1].Value.ToString();
                    string str2 = dgv_IP2.Rows[i].Cells[2].Value.ToString();

DataRow row = DataSet_IP.Tables[0].NewRow();
                    row["IP"] = str1.ToString();
                    row["机号"] = str2.ToString();
                    DataSet_IP.Tables[0].Rows.Add(row);
                }
            }
        }

//// dgv_IP2 Selected
        private void dgv_IP2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.ColumnIndex == 1)
            //{
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                //DataGridViewCell cell = dgv_IP2.Rows[e.RowIndex].Cells[e.ColumnIndex];
                //DataGridViewCell cell = dgv_IP2.Rows[e.RowIndex].Cells[1];
                //MessageBox.Show(cell.ToString());
                //MessageBox.Show(dgv_IP2.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString());

if (dgv_IP2.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString() == "True")
                {
                    str_ip = dgv_IP2.Rows[e.RowIndex].Cells[1].Value.ToString();
                    connection1();
                }
                else
                {
                    str_ip = dgv_IP2.Rows[e.RowIndex].Cells[1].Value.ToString();
                    disconnection1();
                }

//
                //this.dgv_IP2.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_IP2_CellClick);

this.dgv_IP2.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_IP2_CellValueChanged);
                //dgv_IP2.Rows[e.RowIndex].Cells[0].Value = false;
                dgv_IP2.CurrentCell = dgv_IP2[1, 0];
                dgv_IP2.BeginEdit(true);

}
        }

private void dgv_IP2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (m_bConnected == false)
            {
                dgv_IP2.Rows[e.RowIndex].Cells[0].Value = false;
            }
            else
            {
                dgv_IP2.Rows[e.RowIndex].Cells[0].Value = true;
            }

}

////