0

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.

thatguy
  • 21,059
  • 6
  • 30
  • 40
loris02
  • 39
  • 5
  • First, how do you put the values in the Datagrid ? – Ximaze C Oct 08 '20 at 09:12
  • The values come from a database. I have to check every value of this column. – loris02 Oct 08 '20 at 09:19
  • @loris02 Please show where you define and assign the collection that you set as `ItemsSource`. – thatguy Oct 08 '20 at 09:23
  • Then you should think slightly differently to do this. Instead of loading a database and then check on the data, Create an object (Let's call it "Object" for the exemple) that is made to contains the data of a line. Get your data int a List of Object (List) and bind this list to the DataGrid. This must do the same job as what you do BUT it will also allow you to check, modificate, format and do many other action on your data even before it is shown. – Ximaze C Oct 08 '20 at 09:25
  • @thatguy This is how I do it: SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataTable dt = new DataTable(); dataAdapter.Fill(dt); Datagrid.ItemsSource = dt.DefaultView; – loris02 Oct 08 '20 at 09:34

1 Answers1

1

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();
thatguy
  • 21,059
  • 6
  • 30
  • 40
  • Is there a possibility to check each cell of this column from the DataGrid? Because I put the values into a DataGrid for editing. – loris02 Oct 09 '20 at 08:01
  • @loris02 Can you please clarify what you what to achieve? The `paraXML` variable contains each value from the _ParaXML_ column or in other words all values of the cells in that `DataGrid` column. What do you want to check from the `DataGrid` and when? – thatguy Oct 09 '20 at 10:03
  • So here's the thing. I take the values in the ```DataGrid``` from a database. My program shall check, if these values of the column "ParaXML" already exist in another database. The user can also change these values in the ```DataGrid```, so I have to check them there and not directly in the DataTable. – loris02 Oct 09 '20 at 11:30