Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys I have adatagridview thata I populate with dadat from an access dB,
when I populate gridview I also perform some calculations and compute new columns dynamically ,I was able to do this using Expression class, now am stuck on how to
use some of those columns created and display result based on some colimns i am unable.Like am trying display "low"and "High" if certain conditions are made in Column 9 the results should be displayed , but this only works for the first row why , what am I doing wrong below is my code .

C#
foreach (DataGridViewRow row in dataGridView2.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        if ((ActualWeight < ((1 - 0.04) * 100)))
        {
            dataGridView2.Rows[RowIndex].Cells[9].Value = "LOW";
        }
        else if ((ActualWeight < ((1 - 0.04) * 100))) { dataGridView2.Rows[RowIndex].Cells[9].Value = "HIGH"; }
    }
}

I have a total of 0-10 columns and I want the last column "REULTS" to contain the outputs, thanks for any advice or suggestions.
EK.
C#
{
Posted
Updated 27-Mar-15 23:55pm
v3
Comments
Why don't you debug and see what is the issue?
Ekona_Pikin 27-Mar-15 22:31pm    
I ve tried @Tadit thats why am here to see if I could get some help

1 solution

In your code to set the value of column 9, you are using a foreach loop to loop through each row, but when you set the value, you aren't setting it on the current row being enumerated.

Change your code to something like this:
C#
foreach (DataGridViewRow row in dataGridView2.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        if ((ActualWeight < ((1 - 0.04) * 100)))
        {
            row.Cells[9].Value = "LOW";
        }
        else if ((ActualWeight > ((1 - 0.04) * 100))) 
        { 
            row.Cells[9].Value = "HIGH"; 
        }
    }
}
 
Share this answer
 
Comments
Ekona_Pikin 28-Mar-15 6:12am    
Thanks@ Member11428137:)
Member 12764477 24-Oct-16 6:32am    
I have database with some columns & one column name as 'status' its data type is 'bit' --containing value=true/false/null ..
in run time it is displaying true only but i want to show it as used and false & null both should display as unused (true--replace used
false&null -- replace unused)
my code is like
private void button6_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "data source=AMULYA-PC;initial catalog=barcode;integrated security=true";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(@"select tblBarcodeTest1.Time,tblBarcodeTest1.Shift,tblBarcodeTest1.InitialCount,tblBarcodeTest1.FinalCount,tblBarcodeTest1.BarcodeName,tblBarcodeTest1.Machine,tblBarcodeTest1.Date,tblBarcodeTest1.Code,tblBarcodeTest1.status,Table_1.Cut_Roll_Code,Table_1.[Ply Width],Table_1.Percentage from tblBarcodeTest1 LEFT JOIN Table_1 ON tblBarcodeTest1.code=Table_1.[Cutting Angle] ", con);
DataSet dt = new DataSet();

sda.Fill(dt);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dt.Tables[0];


foreach (DataColumn col in dt.Tables[0].Columns)
{
dataGridView1.Columns.Add(col.ColumnName, col.ColumnName);
dataGridView1.Columns[col.ColumnName].DataPropertyName = col.ColumnName;
}
dataGridView1.CellFormatting += dataGridView1_CellFormatting;
dataGridView1.CellParsing += dataGridView1_CellParsing;

}
i used another code but i got error at tryparse

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "status")
            {
                bool Used;
                bool.TryParse(e.Value.ToString(), out Used);
                if (Used)
                    e.Value = "Used";
                else
                    e.Value = "Unused";
             
            }
        }

can anybody help me, am new to this C#.net
thanks..

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