Click here to Skip to main content
15,076,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I have a datagridview in my form. I have added a new textbox column to datagrid.

I want to copy values from existing column(DCOFFERVALUE) in datagrid to the newly added column(DCAMOUNTOREDEEM).
But my new column cells are coming empty.

I don't know what I am missing :(

What I have tried:

New column(txtboxcolumn) added to datagridview :

C#
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.redeemColumn
           });
            this.redeemColumn.HeaderText = "Offer";
            this.redeemColumn.Name = Constants.DCAMOUNTOREDEEM;

Trying to copy values from one col to another in populate grid view method:

foreach (DataGridViewRow dr in dataGridView1.Rows)
{
dataGridView1.Rows[dr.Index].Cells[Constants.DCAMOUNTOREDEEM].Value= dataGridView1.Rows[dr.Index].Cells[Constants.DCOFFERVALUE].Value.ToString();
}
Posted
Updated 31-Jan-21 23:57pm
v3

Because your code doesn't compile, so what you are running isn't the code you think it is.
Your line of code inside the foreach loop needs to be terminated with a semicolon - and until it is, it won't compile, so no EXE file is generated.
Without the system producing a new EXE file, the app you run to test it is the old version, which probably doesn't contain the loop at all!

But why are you doing it like that?
You have a foreach loop that processes each row in turn - so why do you use that row to fetch it's row index and use that to find the row?
Try this instead:
C#
foreach (DataGridViewRow dr in dataGridView1.Rows)
    {
    dr.Cells[Constants.DCAMOUNTOREDEEM].Value = dr.Cells[Constants.DCOFFERVALUE].Value;
    }


And do your testing in the debugger: it will tell you the EXE isn't up-to-date and allow you to look at your code line by line when it doesn't work properly - so you can see what is going wrong and fix it!
   
Comments
Member 13262639 31-Jan-21 8:46am
   
Sorry, I missed the semicolon while posting. But I used it in code. However the above fix didn't work :(
OriginalGriff 31-Jan-21 9:03am
   
So the code you showed us wasn't the code you were running?
That's not very helpful - you should always copy'n'paste code which actually shows the problem, not try to type in "something like it".

What does the debugger show you is happening with your actual code?
Member 13262639 31-Jan-21 23:59pm
   
It is actually showing me the value in that code line. But not able to see it in screen. My columns are empty
Member 13262639 1-Feb-21 0:05am
   
DataGridViewTextBoxColumn txtbox = new DataGridViewTextBoxColumn();
txtbox.Name = Constants.DCAMOUNTOREDEEM;
txtbox.HeaderText = "Redeem Amount";
this.dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
dataGridView1.Columns.Add(txtbox);
foreach (DataGridViewRow dr in dataGridView1.Rows)
{

dr.Cells[Constants.DCAMOUNTOREDEEM].Value = dr.Cells[Constants.DCOFFERVALUE].Value.ToString();

}
Well, i'd recommend to use DataGridView bound to DataTable object, then you'll be able to use Expression[^].

See:
C#
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
	{
		new DataColumn("Price", typeof(double)),
		new DataColumn("Vat", typeof(double)),
		new DataColumn("PriceInclVat", typeof(double)),
	});
	
dt.Columns[2].Expression = "Price + (Price * Vat)";

dt.Rows.Add(new object[]{120.5, 0.22});
dt.Rows.Add(new object[]{10, 0.07});
dt.Rows.Add(new object[]{523.32, 0.22});
dataGridView1.DataSource = dt;


This produces below result:
Price    Vat    PriceInclVat
120.5    0.22   147.01 
10       0.07   10.7 
523.32   0.22   638.4504 
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900