简体   繁体   中英

Error"Cannot implicitly convert type 'string' to 'System.Type'

I am getting an error for the sorting in the format "dd-mmm-yyyy" it should sort date,month,year wise when binding to grid

"Cannot implicitly convert type 'string' to 'System.Type'"

DataView dw = PurchaseRequestDetails.Tables[0].DefaultView;
DateTime NowDate = System.DateTime.Now;
string NewDate = NowDate.ToString("dd-MMM-yyyy");
dw.Table.Columns["RequiredDate"].DataType = NewDate.ToString();

dw.Sort = "RequiredDate ASC";
foreach (DataRowView dr in dw)
{
    System.Diagnostics.Debug.WriteLine("RequiredDate: " + dr["RequiredDate"].ToString());
}

dgvPurReq.DataSource = dw;
dgvPurReq.DataBind();

The error is in the line: dw.Table.Columns["RequiredDate"].DataType = NewDate.ToString();

The DataType property gets or sets the type of data stored in the selected column. http://msdn.microsoft.com/en-us/library/system.data.datacolumn.datatype.aspx

you are setting the data type of the column to the contents of the string which is the current date-time. The current date-time is NOT a valid type. Also, I am pretty sure that changing the data-type of an existing column with data in it will throw an exception as well.

if you remove these three lines

DateTime NowDate = System.DateTime.Now;
string NewDate = NowDate.ToString("dd-MMM-yyyy");
dw.Table.Columns["RequiredDate"].DataType = NewDate.ToString();

I suspect this will work the way you are trying to get it to work.

string NewDate = NowDate.ToString("dd-MMM-yyyy");
dw.Table.Columns["RequiredDate"].DataType = NewDate.ToString();

The second line is key; you're assinging a string ( NewDate.ToString() ) to a property that accepts a Type as a value.

In .NET everything has a Type , but there are also instances of System.Type . It's basically a special/specific class which describes another class (but isn't actually an instance of that class). A rough simile is that it's like a blueprint--it describes something but it's not the real thing.h

To retrieve an instance of a Type you can use the typeof() operator

You can fix it by setting the DataType to a type using typeof(DateTime) or DateTime.Now.GetType(). But what I dont understand is why do you do that? The DataView is returned with a list of rows and columns and from the code above, you are just using it, why do you want to change the data type? Perhaps what you want is to format the data on display?

Please include the line number of the error next time... as that helps.
It seems that you're assigning a string to the DataType property of DataColumn, which expects a Type object. So the fix would be...

dw.Table.Columns["RequiredDate"].DataType = NewDate.GetType(); // or typeof(DateTime)

Update (for problem#2 - sort doesn't happen) Adapted from the code snippet from this MSDN Page (look here for implementation of the helper methods). Formatting of the DataColumn should be the responsibility of the presentation layer/consumer. However if you really need to do this in the DataView - link

private static void DemonstrateDataView(){
   // Create one DataTable with one column.
   DataTable myTable = new DataTable("myTable");
   DataColumn colItem = new DataColumn("item",typeof(DateTime));

  myTable.Columns.Add(colItem);
   // Add five items.
   DataRow NewRow;
   for(int i = 0; i <5; i++){
      NewRow = myTable.NewRow();
      NewRow["item"] = DateTime.Now.AddDays(-i);
      myTable.Rows.Add(NewRow);
   }
   myTable.AcceptChanges();
   // Print current table values.
   PrintTableOrView(myTable,"Current Values in Table");

   DataView secondView = new DataView(myTable);
   secondView.Sort = "item";

   PrintTableOrView(secondView, "Second DataView: ");
}

Output:

Current Values in Table
        8/10/2010 11:34:28 AM
        8/9/2010 11:34:28 AM
        8/8/2010 11:34:28 AM
        8/7/2010 11:34:28 AM
        8/6/2010 11:34:28 AM

Second DataView: 
        8/6/2010 11:34:28 AM
        8/7/2010 11:34:28 AM
        8/8/2010 11:34:28 AM
        8/9/2010 11:34:28 AM
        8/10/2010 11:34:28 AM

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