[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to cast/convert a datatablecolumn  to string ?

Posted on 2007-11-26
8
Medium Priority
?
898 Views
Last Modified: 2013-11-07

How to cast/convert a datatablecolumn  to string ?

["import_policeid"]is a typeof integer

parentView.Table.Columns["import_policeid"].Expression = "Convert(["import_policeid"],System.String)";

throws this exception
Property Expression can not be used , because there is a cirkular reference in the expression

Thanks
0
Comment
Question by:vbturbo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:bungHoc
ID: 20349332
Table.Columns["Import_policeID"].ToString() should do the job.
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 20349356
Another way to do it is to use the Convert class.  It is a class that contains static methods that will convert any object to the requested type (it throws an exception if the type is incompatible, i.e. convert int to date).

So to convert 'import_policeid' to string you would do this:
   string strImportPoliceId=Convert.ToString(parentView.Table.Rows[rowNum]["import_policeid"]);
0
 
LVL 18

Author Comment

by:vbturbo
ID: 20349407
MogalManic:

The reason i want convert the ["import_policeid"] to string is that i need to apply a row filter on a column which datatype is integer.

.RowFilter = "import_policeid Like '%" & txtsearch.Text & "%'"

and i cannot use the LIKE operator on a column that is type of int.

bungHoc:

Your line states that " Value type of string cannot be converted to a dataview
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 7

Expert Comment

by:bungHoc
ID: 20349493
Okay.. thought you just need to convert the entire column to string. But if what you needed is to convert it then import back to DataTable that line won't work.

From top of my head, this is the way to set DataColumn's datatype:
Table.Columns["Import_PoliceID"].DataType = System.Type.GetType("System.String");

If it doesn't work let me know, get back to you in a couple of hours when I get back to my workstation with a running C#.
0
 
LVL 18

Author Comment

by:vbturbo
ID: 20349555
bungHoc:

It Throws the following exception:
Datatype for a column can not be altered when the column contains data.

at the middle of this page it shows how to convert a datatable column

but where am i dropping the ball ?

https://msdn2.microsoft.com/en-us/library/system.data.datacolumn.expression(VS.71).aspx

The following functions are also supported:

CONVERT

Description Converts given expression to a specified .NET Framework Type.
Syntax Convert(expression, type)
Arguments expression-- The expression to convert.
type-- The .NET Framework type to which the value will be converted.
 

Example: myDataColumn.Expression="Convert(total, 'System.Int32')"

Thanks
0
 
LVL 3

Expert Comment

by:j-w-thomas
ID: 20349878
Hi vbturbo,

I am sure that you have already thought of this, but if the column is an integer value and you want to filter by number, can you use greater than or less than:

objDS.RowFilter = "import_policeid > 50"

I have not tried this, so I may be out-to-lunch

John
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 2000 total points
ID: 20350540
If you actually want to use the like operator you can use the Convert function inside the filter like this:
  objDs.RowFilter = "Convert(import_policeid, 'System.String')  Like '%" & txtsearch.Text & "%'"

which will return rows that "contain" the value of txtsearch.Text.  For example if import_policeid datavalues were
  100
  200
  205
  304
  0
  2
and txtsearch.text was "2" then the result set would be
  200
  205
  2
0
 
LVL 18

Author Comment

by:vbturbo
ID: 20352903
MogalManic:

That did it

Thanks to all for colaborating
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question