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

3
我正在用C#开发移动应用程序。我想在数据网格控件的每一行开头动态添加复选框控件,并根据该特定复选框的选择触发事件。我想添加复选框列,并根据任何特定复选框的选择,在选择了该复选框的行上触发事件。我正在使用以下代码。
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个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接