Monday, 2 April 2012

To create columns in run time to bind the grid in Windows C#

 try
            {
                DataGridViewTextBoxColumn dgvtbcTerm = new DataGridViewTextBoxColumn();
                DataGridViewTextBoxColumn dgvtbcDownPaymentAmount2 = new DataGridViewTextBoxColumn();
                DataGridViewTextBoxColumn dgvtbcDownPaymentAmount3 = new DataGridViewTextBoxColumn();
                DataGridViewTextBoxColumn dgvtbcDownPaymentAmount4 = new DataGridViewTextBoxColumn();
                DataGridViewCellStyle style = new DataGridViewCellStyle();
                style.BackColor = Color.Beige;
                DataGridViewCellStyle dgvcHeaderStyle = new DataGridViewCellStyle();
                dgvcHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                DataGridViewCellStyle dgvcStyle = new DataGridViewCellStyle();
                dgvcStyle.Alignment = DataGridViewContentAlignment.BottomRight;

                dgvtbcTerm.Name = "Term";
                dgvtbcTerm.HeaderText = "Term";
                dgvtbcTerm.DataPropertyName = "Term";
                dgvtbcTerm.ReadOnly = true;
                dgvtbcTerm.Width = 115;
                dgvtbcTerm.HeaderCell.Style = dgvcHeaderStyle;
                dgvtbcTerm.CellTemplate = new DataGridViewTextBoxCell();

                dgvtbcDownPaymentAmount2.Name = "DownPaymentAmount2";
                dgvtbcDownPaymentAmount2.HeaderText = "Down Payment Amount * 2";
                dgvtbcDownPaymentAmount2.DataPropertyName = "DownPaymentAmount2";
                dgvtbcDownPaymentAmount2.ReadOnly = true;
                dgvtbcDownPaymentAmount2.Width = 200;
                dgvtbcDownPaymentAmount2.ValueType = typeof(decimal);
                dgvtbcDownPaymentAmount2.HeaderCell.Style = dgvcHeaderStyle;
                dgvtbcDownPaymentAmount2.DefaultCellStyle = dgvcStyle;
                dgvtbcDownPaymentAmount2.CellTemplate = new DataGridViewTextBoxCell();

                dgvtbcDownPaymentAmount3.Name = "DownPaymentAmount3";
                dgvtbcDownPaymentAmount3.HeaderText = "Down Payment Amount * 3";
                dgvtbcDownPaymentAmount3.DataPropertyName = "DownPaymentAmount3";
                dgvtbcDownPaymentAmount3.ReadOnly = true;
                dgvtbcDownPaymentAmount3.Width = 200;
                dgvtbcDownPaymentAmount3.ValueType = typeof(decimal);
                dgvtbcDownPaymentAmount3.HeaderCell.Style = dgvcHeaderStyle;
                dgvtbcDownPaymentAmount3.DefaultCellStyle = dgvcStyle;
                dgvtbcDownPaymentAmount3.CellTemplate = new DataGridViewTextBoxCell();


                dgvtbcDownPaymentAmount4.Name = "DownPaymentAmount4";
                dgvtbcDownPaymentAmount4.HeaderText = "Down Payment Amount * 4";
                dgvtbcDownPaymentAmount4.DataPropertyName = "DownPaymentAmount4";
                dgvtbcDownPaymentAmount4.ReadOnly = true;
                dgvtbcDownPaymentAmount4.Width = 200;
                dgvtbcDownPaymentAmount4.ValueType = typeof(decimal);
                dgvtbcDownPaymentAmount4.HeaderCell.Style = dgvcHeaderStyle;
                dgvtbcDownPaymentAmount4.DefaultCellStyle = dgvcStyle;
                dgvtbcDownPaymentAmount4.CellTemplate = new DataGridViewTextBoxCell();

                dgvLeaseRentalSuggestions.AutoGenerateColumns = false;
                dgvLeaseRentalSuggestions.Enabled = false;
                dgvLeaseRentalSuggestions.DefaultCellStyle = style;
                dgvLeaseRentalSuggestions.Columns.AddRange(new DataGridViewColumn[] { dgvtbcTerm, dgvtbcDownPaymentAmount2, dgvtbcDownPaymentAmount3, dgvtbcDownPaymentAmount4 });
                dgvLeaseRentalSuggestions.BackgroundColor = Color.Beige;
                dgvLeaseRentalSuggestions.Columns["DownPaymentAmount2"].DefaultCellStyle.Format = "#,0.00";
                dgvLeaseRentalSuggestions.Columns["DownPaymentAmount3"].DefaultCellStyle.Format = "#,0.00";
                dgvLeaseRentalSuggestions.Columns["DownPaymentAmount4"].DefaultCellStyle.Format = "#,0.00";


            }

private void SetDetfaultValuesToLeaseRentalSuggestionsGrid()
        {
            _dsLeaseRentalSuggestions = new DataSet();
            DataTable dtLeaseRentalSuggestions = new DataTable();
            dtLeaseRentalSuggestions.Columns.Add("Term", typeof(Int32));
            dtLeaseRentalSuggestions.Columns.Add("DownPaymentAmount2", typeof(Decimal));
            dtLeaseRentalSuggestions.Columns.Add("DownPaymentAmount3", typeof(Decimal));
            dtLeaseRentalSuggestions.Columns.Add("DownPaymentAmount4", typeof(Decimal));
            dtLeaseRentalSuggestions.TableName = "LeaseRentalSuggestions";
            _dsLeaseRentalSuggestions.Tables.Add(dtLeaseRentalSuggestions);

            DataRow drLeaseRentalSuggestions = dtLeaseRentalSuggestions.NewRow();
            drLeaseRentalSuggestions["Term"] = 60;
            dtLeaseRentalSuggestions.Rows.Add(drLeaseRentalSuggestions);
            drLeaseRentalSuggestions = dtLeaseRentalSuggestions.NewRow();
            drLeaseRentalSuggestions["Term"] = 48;
            dtLeaseRentalSuggestions.Rows.Add(drLeaseRentalSuggestions);
            drLeaseRentalSuggestions = dtLeaseRentalSuggestions.NewRow();
            drLeaseRentalSuggestions["Term"] = 36;
            dtLeaseRentalSuggestions.Rows.Add(drLeaseRentalSuggestions);
            dgvLeaseRentalSuggestions.DataSource = _dsLeaseRentalSuggestions.Tables["LeaseRentalSuggestions"];
            _dsLeaseRentalSuggestions.Tables["LeaseRentalSuggestions"].DefaultView.AllowNew = false;
        }

No comments:

Post a Comment