SQL 2

来源:互联网 发布:猪八戒淘宝收藏有用吗 编辑:程序博客网 时间:2024/05/18 23:53

//在datagridview中添加复选框

 

private:System::Collections::Generic::Dictionary<int, bool> MyCheckStates;//创建表示键和值的集合

 
private:void UpdateStatuesBar(){
             int MyCount = 0;

             for each(bool IsChecked in MyCheckStates.Values){
                if(IsChecked)
                    MyCount++;
             }
             this->toolStripStatusLabel1->Text = "当前选择的记录条数:" + MyCount.ToString(System::Globalization::CultureInfo::CurrentUICulture);//根据本地区域习惯格式化字符串
        }


//打开数据库
    private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
                 try{
                     this->sqlConnection1 = gcnew SqlConnection ("Data Source=CZ;Initial Catalog=mysql2005;User ID=sa;Password=jianxin198563;Integrated Security =SSPI;Asynchronous Processing=True");
                     this->sqlConnection1->Open();
                     this->sqlCommand1 = gcnew SqlCommand("select * from Character", sqlConnection1);
                     //this->sqlDataAdapter1->SelectCommand = this->sqlCommand1;
                    
                     IAsyncResult^ MyResult = this->sqlCommand1->BeginExecuteReader();


                     while(MyResult->IsCompleted){
                         System::Threading::Thread::Sleep(10);
                     }

                     SqlDataReader^ MyReader = this->sqlCommand1->EndExecuteReader(MyResult);
                     DataTable^ MyTable = gcnew DataTable();
                     MyTable->Load(MyReader);
                    


                     this->sqlDataAdapter1->SelectCommand = this->sqlCommand1;
                     this->sqlDataAdapter1->Fill(this->dataSet1,"Character");

                     this->dataGridView1->DataSource = MyTable;        
                
                 }
                 catch(Exception^ eee){
                    MessageBox::Show(eee->Message, "", MessageBoxButtons::OK, MessageBoxIcon::Information);
                 }
                 finally{
                     this->sqlConnection1->Close();
                 }

             }


private: System::Void dataGridView1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
             if(this->dataGridView1->CurrentCellAddress.X != 0){//获取单元格的行和列索引 即如果点击的单元格不是表格的第0行
                 if(e->KeyCode == Keys::Space){  //按下空格键
                     bool checkedValue = (bool)dataGridView1->Rows[dataGridView1->CurrentCellAddress.Y]->Cells[0]->Value;
                     this->dataGridView1->Rows[dataGridView1->CurrentCellAddress.Y]->Cells[0]->Value = !checkedValue;
                     //对单元格所在行的第一个单元格的值取反
                 }
             }
         }


private: System::Void dataGridView1_CellContentClick(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
             if(e->ColumnIndex == 0)//如果点击第0列的单元格
                 dataGridView1->Rows[e->RowIndex]->Cells[0]->Value = (bool)dataGridView1->Rows[e->RowIndex]->Cells[0]->EditedFormattedValue;
            //则将此单元格的值初始化
         }


private: System::Void dataGridView1_CellValueChanged(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
             if(e->ColumnIndex == 0 && e->ColumnIndex != -1){ //如果数据表不存在或为空
                 int MyFirstColumn = (int)dataGridView1->Rows[e->RowIndex]->Cells[1]->Value;
                 MyCheckStates[MyFirstColumn] = (bool)dataGridView1->Rows[e->RowIndex]->Cells[0]->Value;
                 this->UpdateStatuesBar();
             }
         }


private: System::Void dataGridView1_CellValueNeeded(System::Object^  sender, System::Windows::Forms::DataGridViewCellValueEventArgs^  e) {
             if(e->ColumnIndex == 0){
                 int MyFirstColumn = (int)dataGridView1->Rows[e->RowIndex]->Cells[1]->Value;
                 if(MyCheckStates.ContainsKey(MyFirstColumn)) //确定MyCheckStates集合中是否包含本行  ContainsKey方法确定 Control.ControlCollection创建的对象 是否包含具有指定键的项。
                     e->Value = MyCheckStates[MyFirstColumn];//e->Value用于获取或设置发生此事件的单元格的值。
                
                 else
                     e->Value = nullptr;
                
         }


private: System::Void dataGridView1_CellValuePushed(System::Object^  sender, System::Windows::Forms::DataGridViewCellValueEventArgs^  e) {
             if(e->ColumnIndex == 0){
                 int MyFirstColumn = (int)dataGridView1->Rows[e->ColumnIndex]->Cells[1]->Value; //返回单元格的所在行的checkbox的值
                 if(!MyCheckStates.ContainsKey(MyFirstColumn)) //如果MyCheckStates集合中不包含本行
                     MyCheckStates.Add(MyFirstColumn, (bool)e->Value);             //在MyCheckStates集合中添加此行的状态
                 else
                     MyCheckStates[MyFirstColumn] = (bool)e->Value; //否则只更新此行的状态
                
             }
         }

private: System::Void dataGridView1_DataBindingComplete(System::Object^  sender, System::Windows::Forms::DataGridViewBindingCompleteEventArgs^  e) {
             dataGridView1->AutoResizeColumn(DataGridViewAutoSizeColumnMode::AllCells); //根据单元格内容调整列宽
         }

原创粉丝点击