none
How Do I Set a Default Value for a DataGridView Column from Code (C#).

    Question

  • I want one of the columns in my gridview to have $0.00 in each cell by default. I did this from code a while ago, but I have forgotten and can't seem to find an example. I am pulling the table from an access database and allowing users to edit/add/delete stuff.
    Tuesday, May 05, 2009 2:08 AM

Answers

  •  

    Hi this is an example where $ is set by default to one of the cells in datagridview
    try this
    u ve to work around this

    private
    void datagridview_CellEndEdit(object sender, DataGridViewCellEventArgs e)

    {

     

    if (datagridview.Rows.Count > 0)

    EndEdit();

    }

     

    private void EndEdit()

    {

     

    string charge = datagridview.CurrentRow.Cells[1].FormattedValue.ToString();which cell ex:1st cell

     

    string valid = "0123456789.\b";

    charge = charge.Replace(

    "$ 0.00", "");

    NewCharge =

    Convert.ToDecimal(charge);

    datagridview.CurrentRow.Cells[ur cell].Value = NewCharge;

    NewCharge =

    Convert.ToDecimal(datagridview.CurrentRow.Cells[ur cell no].FormattedValue);

    NewProcedurecode = datagridview.CurrentRow.Cells[

    "ur cell"].FormattedValue.ToString();

    datagridview.EndEdit();

    ProviderId =

    Convert.ToInt32(datagridview.CurrentRow.Cells["providerIdDataGridViewTextBoxColumn"].FormattedValue);

    others u need to put in

    this is how u can set $0.00 in dgv cel


    Meghana Lohit
    • Marked as answer by Kira Qian Monday, May 11, 2009 5:46 AM
    Tuesday, May 05, 2009 3:41 AM
  • Hi Zero_gt,

    You can create an inherited DataGridViewColumn, DataGridViewTextBoxCell and override the DefaultNewRowValue property to implement this. Here is the sample code.
    MyTextBoxColumn code:
    public class MyTextBoxColumn : DataGridViewColumn
        {
            public MyTextBoxColumn()
                : base(new MyTextBoxCell())
            {
            }
        }

    MyTextBoxCell code:
    public class MyTextBoxCell : DataGridViewTextBoxCell
        {
            public MyTextBoxCell() : base()
            {
            }

            public override object DefaultNewRowValue
            {
                get
                {
                    return "$0.00";
                }
            }
        }

    To use this column, you can create an instance and add it to the DataGridView.
    MyTextBoxColumn myTextBoxColumn = new MyTextBoxColumn();
    myTextBoxColumn.HeaderText = "Column1";
    dataGridView1.Columns.Add(myTextBoxColumn);

    So that column will always show "$0.00" for the new row.

    Please tell me if you need any help.

    Sincerely,
    Kira Qian


    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by Kira Qian Monday, May 11, 2009 5:44 AM
    Thursday, May 07, 2009 3:46 AM

All replies

  •  

    Hi this is an example where $ is set by default to one of the cells in datagridview
    try this
    u ve to work around this

    private
    void datagridview_CellEndEdit(object sender, DataGridViewCellEventArgs e)

    {

     

    if (datagridview.Rows.Count > 0)

    EndEdit();

    }

     

    private void EndEdit()

    {

     

    string charge = datagridview.CurrentRow.Cells[1].FormattedValue.ToString();which cell ex:1st cell

     

    string valid = "0123456789.\b";

    charge = charge.Replace(

    "$ 0.00", "");

    NewCharge =

    Convert.ToDecimal(charge);

    datagridview.CurrentRow.Cells[ur cell].Value = NewCharge;

    NewCharge =

    Convert.ToDecimal(datagridview.CurrentRow.Cells[ur cell no].FormattedValue);

    NewProcedurecode = datagridview.CurrentRow.Cells[

    "ur cell"].FormattedValue.ToString();

    datagridview.EndEdit();

    ProviderId =

    Convert.ToInt32(datagridview.CurrentRow.Cells["providerIdDataGridViewTextBoxColumn"].FormattedValue);

    others u need to put in

    this is how u can set $0.00 in dgv cel


    Meghana Lohit
    • Marked as answer by Kira Qian Monday, May 11, 2009 5:46 AM
    Tuesday, May 05, 2009 3:41 AM
  • I am getting close, and I am trying to do this:
    dataGridView1.Columns[6].CellTemplate.Value = 0.00;

    -But so far the cells in the column are just empty.
    Wednesday, May 06, 2009 3:07 AM
  • This is an example try it private void LoadBillingDetails() { decimal total = 0.00m; if (gridview.Rows.Count > 0) { foreach (DataGridViewRow dgvr in datgridview.Rows) { total += Convert.ToDecimal(dgvr.Cells[ur cell no].Value); string tot = "$" + dgvr.Cells[ur cell no].Value; } if ((DataGridViewRow .Rows.Count > 0) { txtPatientCopay.Text = "$" + invoiceinfo[0].PatientCoPay.ToString(); txtInsurancePortion.Text = "$" + invoiceinfo[0].InsurancePortion.ToString(); txtBillingBalance.Text = "$" + invoiceinfo[0].BillingBalance.ToString(); chcboxCoPayPayment.Checked = invoiceinfo[0].CoPayPaymentReceived; txtTotal.Text = "$" + total.ToString(); if (datagridview.Rows.Count > 0) for (int i = 0; i < info.Count; i++) { if (info[i].PatientInsuranceId.Equals(Convert.ToInt32(dgBillDetails.Rows[0].Cells[20].Value))) { cbSelectInsurance.Text = info[i].AddCoverageType; EnableFields(); cbSelectInsurance.Enabled = false; break; } else { cbSelectInsurance.Enabled = true; } } } else { txtTotal.Text = "$" + total.ToString(); //txtTotal.Text = "$00.00"; txtPatientCopay.Text = "$00.00"; txtInsurancePortion.Text = "$00.00"; chcboxCoPayPayment.Checked = false; chcboxInsurancePayment.Checked = false; cbSelectInsurance.Text = " - "; cbSelectInsurance.Enabled = true; } } else { cbSelectInsurance.Enabled = true; } }
    Meghana Lohit
    Wednesday, May 06, 2009 10:20 AM
  • Hi Zero_gt,

    You can create an inherited DataGridViewColumn, DataGridViewTextBoxCell and override the DefaultNewRowValue property to implement this. Here is the sample code.
    MyTextBoxColumn code:
    public class MyTextBoxColumn : DataGridViewColumn
        {
            public MyTextBoxColumn()
                : base(new MyTextBoxCell())
            {
            }
        }

    MyTextBoxCell code:
    public class MyTextBoxCell : DataGridViewTextBoxCell
        {
            public MyTextBoxCell() : base()
            {
            }

            public override object DefaultNewRowValue
            {
                get
                {
                    return "$0.00";
                }
            }
        }

    To use this column, you can create an instance and add it to the DataGridView.
    MyTextBoxColumn myTextBoxColumn = new MyTextBoxColumn();
    myTextBoxColumn.HeaderText = "Column1";
    dataGridView1.Columns.Add(myTextBoxColumn);

    So that column will always show "$0.00" for the new row.

    Please tell me if you need any help.

    Sincerely,
    Kira Qian


    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by Kira Qian Monday, May 11, 2009 5:44 AM
    Thursday, May 07, 2009 3:46 AM
  • This is an example try it private void LoadBillingDetails() { decimal total = 0.00m; if (gridview.Rows.Count > 0) { foreach (DataGridViewRow dgvr in datgridview.Rows) { total += Convert.ToDecimal(dgvr.Cells[ur cell no].Value); string tot = "$" + dgvr.Cells[ur cell no].Value; } if ((DataGridViewRow .Rows.Count > 0) { txtPatientCopay.Text = "$" + invoiceinfo[0].PatientCoPay.ToString(); txtInsurancePortion.Text = "$" + invoiceinfo[0].InsurancePortion.ToString(); txtBillingBalance.Text = "$" + invoiceinfo[0].BillingBalance.ToString(); chcboxCoPayPayment.Checked = invoiceinfo[0].CoPayPaymentReceived; txtTotal.Text = "$" + total.ToString(); if (datagridview.Rows.Count > 0) for (int i = 0; i < info.Count; i++) { if (info[i].PatientInsuranceId.Equals(Convert.ToInt32(dgBillDetails.Rows[0].Cells[20].Value))) { cbSelectInsurance.Text = info[i].AddCoverageType; EnableFields(); cbSelectInsurance.Enabled = false; break; } else { cbSelectInsurance.Enabled = true; } } } else { txtTotal.Text = "$" + total.ToString(); //txtTotal.Text = "$00.00"; txtPatientCopay.Text = "$00.00"; txtInsurancePortion.Text = "$00.00"; chcboxCoPayPayment.Checked = false; chcboxInsurancePayment.Checked = false; cbSelectInsurance.Text = " - "; cbSelectInsurance.Enabled = true; } } else { cbSelectInsurance.Enabled = true; } }
    Meghana Lohit

    I hope you don't mind my expanding on your example.  I had to put it into what, for me, is more readable form:

    private void LoadBillingDetails()
    {	decimal total = 0.00m;
    	if (gridview.Rows.Count > 0)
    	{
    		foreach (DataGridViewRow dgvr in datgridview.Rows)
    			{
    				total += Convert.ToDecimal(dgvr.Cells[ur cell no].Value);
    				string tot = "$" + dgvr.Cells[ur cell no].Value;
    			}
    		if ((DataGridViewRow .Rows.Count > 0)
    		{
    			txtPatientCopay.Text = "$" + invoiceinfo[0].PatientCoPay.ToString();
    			txtInsurancePortion.Text = "$" + invoiceinfo[0].InsurancePortion.ToString();
    			txtBillingBalance.Text = "$" + invoiceinfo[0].BillingBalance.ToString();
    			chcboxCoPayPayment.Checked = invoiceinfo[0].CoPayPaymentReceived;
    			txtTotal.Text = "$" + total.ToString();
    			if (datagridview.Rows.Count > 0)
    				for (int i = 0; i < info.Count; i++)
    				{
    					if (info[i].PatientInsuranceId.Equals(Convert.ToInt32(dgBillDetails.Rows[0].Cells[20].Value)))
    					{
    						cbSelectInsurance.Text = info[i].AddCoverageType;
    						EnableFields();
    						cbSelectInsurance.Enabled = false;
    						break;
    					}
    					else 
    					{ 
    						cbSelectInsurance.Enabled = true;
    					}
    				} 
    		}
    		else
    		{
    			txtTotal.Text = "$" + total.ToString();//txtTotal.Text = "$00.00"; 
    			txtPatientCopay.Text = "$00.00";
    			txtInsurancePortion.Text = "$00.00";
    			chcboxCoPayPayment.Checked = false;
    			chcboxInsurancePayment.Checked = false;
    			cbSelectInsurance.Text = " - ";
    			cbSelectInsurance.Enabled = true;
    		}
    	}
    	else
    	{
    		cbSelectInsurance.Enabled = true;
    	}
    } 

    Rod
    Friday, October 09, 2009 7:54 PM
  • private void dbGridView_DefaultValuesNeeded(object sender,
    DataGridViewRowEventArgs e)
    {
    e.Row.Cells["Price"].Value = (decimal)0;
    }
    Thursday, April 07, 2011 6:21 AM
  • I know this is an old thread but for those using dataTable as datasource for a DataGridview there is an easy quick way:

    theDataTable.Columns["Price"].DefaultValue = (decimal)0;

    Otherwise, one could try Vildan3003's suggestion

    myDataGridview.DefaultValuesNeeded += new DataGridViewRowEventHandler(myDataGridview_DefaultValuesNeeded);
    private void myDataGridview_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
     e.Row.Cells["Price"].Value = (decimal)0;
    }



    • Edited by fs - ab Monday, May 14, 2012 5:38 AM
    Monday, May 14, 2012 5:15 AM