Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai. I'm having a problem on this code. i tried to check if the data is empty or not in order to bind it to datagridview. this is the code so far

C#
dbSumMetName.RowCount = form3.dbMetName.RowCount;
         foreach (DataGridViewRow row in form3.dbMetName.Rows)
         {
             foreach (DataGridViewColumn col in form3.dbMetName.Columns)
             {
                 {
                     dbSumMetName.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
                 }
             }
         }


For this code, i try to make it like this

C#
dbSumMetName.RowCount = form3.dbMetName.RowCount;
         foreach (DataGridViewRow row in form3.dbMetName.Rows)
         {
             foreach (DataGridViewColumn col in form3.dbMetName.Columns)
             {
                 {
                     if (row.Cells[col.Name].Value != null)
                     dbSumMetName.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
                 }
             }
         }

it still showing the empty space.
Posted
Comments
Krunal Rohit 2-Mar-14 9:20am    
So you want to remove the column that has null value, right ? So what if there are 3 columns which contains null value ?

-KR
arave0521 2-Mar-14 9:22am    
yes. the first form actually allowed user to add how many row he want. but when it comes to second form which showing the datagridview which only allowed user to edit and not allowed him to add at all.

Well... um.
Let's look at this logically:
C#
foreach (DataGridViewRow row in form3.dbMetName.Rows)
{
...
}
Looks at every row
C#
foreach (DataGridViewColumn col in form3.dbMetName.Columns)
{
...
}
Looks at every column in every row.
And then:
C#
if (row.Cells[col.Name].Value != null)
dbSumMetName.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;

So...what value are you putting in a cell if the value you don't want to put in the cell is null?

Nothing at all.

Otherwise known as null...so if the "new" value is null, you leave the old, null value...
Perhaps, you want something like this:
C#
foreach (DataGridViewRow row in form3.dbMetName.Rows)
{
    foreach (DataGridViewColumn col in form3.dbMetName.Columns)
    {
        if (row.Cells[col.Name].Value != null)
        {
            dbSumMetName.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
        }
        else
        {
            dbSumMetName.Rows[row.Index].Cells[col.Name].Value = "????";
        }
    }
}



"how to remove 1 whole row if let say there are two column and two column has no value. some sort like this code"
C#
DataGridView1.Rows.Remove(_row);




Well...first thing is: you can't use foreach. It would require modifying the collection, and that is not allowed within a foreach.
So, if you want to get rid of the whole row if all it's columns are empty:
C#
for (int i = form3.dbMetName.Rows.Count - 1; i >= 0; i--)
    {
    bool isBlank = true;
    DataGridViewRow row = form3.dbMetName.Rows[i];
    foreach (DataGridViewCell cell in row.Cells)
        {
        if (cell.Value != null && cell.Value.ToString() != "")
            {
            isBlank = false;
            break;
            }
        }
    if (isBlank)
        {
        form3.dbMetName.Rows.Remove(row);
        }
    }

Going down from the last row to the first is safer than going up, as you remove rows and shuffle ones you have checked!
 
Share this answer
 
v2
Comments
arave0521 2-Mar-14 10:15am    
how to remove 1 whole row if let say there are two column and two column has no value. some sort like this code
DataGridView1.Rows.Remove(_row);
OriginalGriff 2-Mar-14 10:34am    
Answer updated
arave0521 2-Mar-14 10:49am    
There is a problem with the code. The code quite similar with the one I tried before and it produce the same error. The error is uncommitted new row cannot be deleted
OriginalGriff 2-Mar-14 11:00am    
When you get an error message as specific as that, don't you think it would be a good idea to mention it? :sigh:

What does the error say? It says that the new row is "uncommitted" and "cannot be deleted" - which means it's a row which hasn't been written to the database yet.
Easiest ways to get rid of it:
1) Don't let the user add rows: set the DataGridView.AllowUserToAddRows = false
2) Cancel the change: Look at the bound table, and do this:
MyTable.Rows(MyDataGridViewRowIndex).RejectChanges()
Obviously, you need to put the row index for your DGV in there...
arave0521 2-Mar-14 11:45am    
it works. just because I did not set the DataGridView.AllowUserToAddRows = false in the function. sorry for not mentioning at first. thank you so much btw.
Okay, I might be wrong, I mean it can't be exact solution as you want but still I'm trying.
See this code will eliminate all the column which contains null values.
C#
public DataTable CheckDataTableColumn(DataTable dt)
{
    bool flag = false;
    int counter = 0;

EXIT:
    for (int i = counter; i < dt.Columns.Count; i++)
    {
        for (int x = 0; x < dt.Rows.Count; x++)
        {
            if (string.IsNullOrEmpty(dt.Rows[x][i].ToString()))
                flag = true;    // empty value
            else
            {
                flag = false;
                counter = i + 1;

                goto EXIT;
            }
        }
        if (flag)
        {
            dt.Columns.Remove(dt.Columns[i]);
            i--;
        }
    }

    return dt;
}

Hope might be helpful to you.

-KR
 
Share this answer
 
Comments
OriginalGriff 2-Mar-14 12:02pm    
Reason for my vote of one: if it wasn't for your adding code that makes this crashs and adding an unnecessary GOTO, how does this significantly differ from the code I posted an hour before you posted this?
Krunal Rohit 2-Mar-14 12:53pm    
I haven't seen your code yet Paul. And about the difference, you already are wise enough and much intellegent than me to identify that.
Cheers :) :)
-KR

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