loris02
loris02

Reputation: 39

How to get all values of a column in WPF?

I have this DataGrid:

Datagrid

I get the data from SQL like this:

SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
Datagrid.ItemsSource = dt.DefaultView;

How can I get all values of the column "ParaXML" from the DataGrid?

Thanks in advance.

Upvotes: 0

Views: 903

Answers (1)

thatguy
thatguy

Reputation: 22089

The values come from a database. I have to check every value of this column

Instead of trying to get all column values from the DataGrid, access the DataTable directly in your code. As you did not provide information on that, I assume that the column data type is string and the column ID is ParaXML. If it is different in your code, please adapt the following samples.

foreach(DataRow row in dt.Rows)
{
   var columnValue = (string)row["ParaXML"];
   // ...do something with the column value.
}

If you want to get all column values as a list instead, you can do it like above in a loop or using Linq.

var paraXmls = new List<string>();
foreach(DataRow row in dt.Rows)
   paraXMLs.Add((string)row["ParaXML"]);
var paraXmls = dt.AsEnumerable().Select(row => row.Field<string>("ParaXML")).ToList();

Upvotes: 1

Related Questions