0

I would like to create the function and pass the values to reduce the duplicate codes in my coding.

It will be called like this in GridView ItemDataBound event

DataRowView drv = e.Row.DataItem as DataRowView;
MyFunc(drv["HasDonePart1"]);
MyFunc(drv["HasDonePart2"]);
MyFunc(drv["HasDonePart3"]);
etc...

The function will be like the following

private void MyFunc ((ClassName) item)
{
    if (item == DBNULL.value)
    {
       //do something
    }
    else
    {
       string myValue = item.ToString();
       //do other things
    }
}

But I don't know what should I put in my method to accept the object.

TTCG
  • 8,805
  • 31
  • 93
  • 141
  • Not enough information. The `DataRowView`'s indexer is declared as returning `object`; what it actually returns depends on the type of the column. You could use a debugger to find out, or print out `drv["HasDonePart1"].GetType().FullName`. – TypeIA Feb 13 '14 at 16:57
  • It returns System.DBNull. Actually it returns the type of the object not the type of the value, right? – TTCG Feb 13 '14 at 16:59

2 Answers2

0

DataRowView.Item indexed property returns object, so that's what your method should take as parameter. See http://msdn.microsoft.com/en-us/library/0k5w061k(v=vs.110).aspx

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

Change MyFunc to accept object, instead:

private void MyFunc(object item)
{
    if (item == DBNull.Value)
    {
        // Do something
    }
    else if (item is string)
    {
    }
    else if (item is decimal)
    {
    }

    // etc.
}
NathanAldenSr
  • 7,841
  • 4
  • 40
  • 51