如何在数据网格控件中每行的开头添加复选框控件?

发布于 2024-09-26 10:48:14 字数 4107 浏览 4 评论 0原文

我正在用 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();

        }         

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

I am developing mobile application in C#. I want to add the checkbox control dynamically at the beginning of every row in the datagrid control & based on that particular checkbox selection I want to fire the event. I want to add the checkbox column & based on the selection of any particular checkbox, I want to fire the event on that checkbox selected row. I am using the following code.

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();

        }         

Can you please provide me any code or link through which I can resolve the above issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文