简体   繁体   中英

Using Operators with two different Types

I have a DataView that is already created. I built a new DataTable from the existing DataView and made that data a DataView to export in an XlSX. I am trying to do check and see if that last column has an Integer that is greater than 0. If it does it should delete the row. Since these are two different types as one is an object and the other is a double it is not as simple as I thought.

I have looked into the answer on Operator '<' cannot be applied to operands of type 'object' and 'int'

In most cases this would have done it, However somewhere it returns a DBNull.Value as stated. If i try to parse it doesn't appear to like it and it wont build.

private DataView CreateExhaustionDataView()
{
    DataTable dt = BuildFufillmentDataTable();
    DataView dv = new DataView(dt, "", "Department,Date", DataViewRowState.CurrentRows);

    DataTable result = dv.ToTable(true,"Department","Date","Remaining");
    var dr = result.NewRow();
    if (Convert.ToDouble(dr["Remaining"]) >= 0.0)
    {
        dr.Delete();
    }
    DataView dvresult = new DataView(result);
    return dvresult;
}

I receive this error message when I try to convert:

System.InvalidCastException: 'Object cannot be cast from DBNull to other types.' I only receive this error when it goes to export the data. As far as parsing it does not build at all.

Any tips or routes I missed I would greatly appreciate it!

It turns out OP wanted, in essence, to filter result .

You're not looking at the first row, you're creating a new row with the same columns as your DataTable, and looking at that. That new row won't even be in the DataTable until you explicitly add it.

var dr = result.NewRow();

The values in dr are all null, since you just created the poor thing. You're also not checking to see if the column is null, which you ought to do.

if (result.Rows.Count > 0)
{
    var firstrow = result.Rows[0];

    if (firstrow["Remaining"] != DBNull.Value 
        && Convert.ToDouble(firstrow["Remaining"]) >= 0.0)
    {
        //  Or maybe firstrow.Delete(), I don't have time to test this. 
        //  If one doesn't work, try the other. 
        result.Rows.Remove(firstrow);
    }
}

I found a way to simply sort the data table without using the For loop as discussed earlier. I commented the For Loop out to test the sorting and actually works just as well without as much lines of code.

private DataView CreateExhaustionDataView()
{
    DataTable dt = BuildFufillmentDataTable();
    DataView dv = new DataView(dt, "", "Department,Date", DataViewRowState.CurrentRows);
    DataTable result = dv.ToTable(true, "Department", "Date", "Remaining");
    /*for (int i= result.Rows.Count-1; i >= 0; i--)
    {
        if (result.Rows.Count > 0)
        {
            var firstrow = result.Rows[i];

            if (firstrow["Remaining"] != DBNull.Value
                && Convert.ToDouble(firstrow["Remaining"]) > 0.0)
            {
                firstrow.Delete();
            }
        }
    }*/ 
    DataView dvresult = new DataView(result, "Remaining <= 0", "Department, Date", DataViewRowState.CurrentRows);
    return dvresult;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM