如何在datagrid控件的每一行的开头添加复选框控件?

时间:2010-10-07 07:55:00

标签: c# datagrid compact-framework checkbox dynamic

我正在用C#开发移动应用程序。我想在datagrid控件&中的每一行的开头动态添加复选框控件。基于特定的复选框选择,我想要触发事件。我想添加复选框列&根据任何特定复选框的选择,我想在该复选框选中的行上触发事件。我使用以下代码。

private void ShowRegistrationKeyDetails_Load(object sender, EventArgs e)
        {
            GridHeight = 40;
            SQLiteDataReader SQLiteDrKeyObj = null;
            DataTable dt = new DataTable();
            DataManager DataMgrObj = new DataManager();
            //int KeyID = Cust_ID;
            //string Client_Key1 = Client_Key;
            int KeyID = Selected_Customer_ID;
            //string Client_Key = Selected_Client_Key;

            SQLiteDrKeyObj = DataMgrObj.getRegistrationKey(KeyID);
            dt.Load(SQLiteDrKeyObj);
            //dt.Columns.Add(new DataColumn("Select", typeof(Boolean)));

            RegKeyInfodataGrid.DataSource = dt;

            SizeColumns(RegKeyInfodataGrid);            
            RegKeyInfodataGrid.Height = GridHeight;

        }

        protected void SizeColumns(DataGrid grid)
        {
            //grid.Controls.Add(new CheckBox());


            Graphics g = CreateGraphics();


            DataTable dataTable = (DataTable)grid.DataSource;


            //DataColumn dtcCheck = new DataColumn("IsMandatory");//create the data          
            ////column object with the name 
            //dtcCheck.DataType = System.Type.GetType("System.Boolean");//Set its 
            ////data Type    
            //dtcCheck.DefaultValue = false;//Set the default value
            //dataTable.Columns.Add(dtcCheck);//Add the above column to the 
            //Data Table
            //Set the Data Grid Source as the Data Table createed above    
            //grid.DataSource = dataTable1;
            // set style property when first time the grid loads, next time onwards // it will maintain its property    


            DataGridTableStyle dataGridTableStyle = new DataGridTableStyle();

            dataGridTableStyle.MappingName = dataTable.TableName;

            int RowCount = dataTable.Rows.Count;
            //foreach(




            foreach (DataColumn dataColumn in dataTable.Columns)
            {
                int maxSize = 0;               
                SizeF size = g.MeasureString(
                                dataColumn.ColumnName,
                                grid.Font
                             );

                if (size.Width > maxSize)
                    maxSize = (int)size.Width;


                //grid.Controls.Add(new CheckBox());

                foreach (DataRow row in dataTable.Rows)
                {
                    size = g.MeasureString(
                              row[dataColumn.ColumnName].ToString(),
                              grid.Font
                        );

                    if (size.Width > maxSize)                    
                        maxSize = (int)size.Width;

                    // AutoResize DataGrid Control
                    string Act_Date = dataColumn.ColumnName;
                    if (Act_Date == "Activation_Date")
                    {
                        GridHeight = GridHeight + 17;

                        //CheckBox chk = new CheckBox();
                        //chk.Location = new Point(20, 30);
                        //this.Controls.Add(chk);
                        //dataTable.Rows.Add(new CheckBox());
                    }

                }                

                DataGridColumnStyle dataGridColumnStyle = new DataGridTextBoxColumn();
                dataGridColumnStyle.MappingName = dataColumn.ColumnName;
                dataGridColumnStyle.HeaderText = dataColumn.ColumnName;
                dataGridColumnStyle.Width = maxSize + 5;
                dataGridTableStyle.GridColumnStyles.Add(dataGridColumnStyle);
            }
            grid.TableStyles.Add(dataGridTableStyle);

            g.Dispose();
        }

        private void BackmenuItem_Click(object sender, EventArgs e)
        {
            QueryDetails QueryDetailsObj = new QueryDetails();
            QueryDetailsObj.Show();

        }         

您能否提供我可以解决上述问题的任何代码或链接?

1 个答案:

答案 0 :(得分:1)