-1

I wana get data row in Gridcontrol from the first to the last row. I tried this code but it did work correctly. When I Debug,

string.Concatreturn :

SELECT * FROM viewdulieu2 WHERE Khachdat = N''

This is my Window.xaml

DataTable a = new DataTable();
        a = ((DataView)ctrlgridviewdulieu0.ItemsSource).ToTable();

        foreach (DataRow row in a.Rows)
        {
            DataTable dtrow = new DataTable();
            dtrow = a.Clone();

            dtrow.ImportRow(row);
            try
            {
                cnn.Open();
                SqlCommand cmd = new SqlCommand(" SELECT * FROM viewdulieu2 WHERE Khachdat = N'" + dtrow + "'", cnn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt1 = new DataTable();
                da.Fill(dt1);
                XtraReport1 report = new XtraReport1();
                report.DataSource = dt1;
                //   report.Print();
                cnn.Close();
                report.ShowPreviewDialog();
            }
            catch (Exception ex)
            {
                cnn.Close();
                MessageBox.Show(ex.Message);
            }

This is my Window.xaml.cs

<dxg:GridControl  
    x:Name="ctrlgridviewdulieu0"  
    AutoGenerateColumns="AddNew" 
    EnableSmartColumnsGeneration="True" 
    HorizontalAlignment="Left" 
    Margin="0,-12,0,-3" 
    VerticalAlignment="Top" 
    Height="592" 
    Width="176">
    <dxg:GridControl.View>
        <dxg:TableView/>
    </dxg:GridControl.View>
</dxg:GridControl>

I seached many document about this proble and find many supports but they always told about Gridview.Getdatarow, Datagrid or something in WinForm in Win but mine is Gridcontrol and WPF which does not contain .Getdatarow property

j.doen
  • 11
  • 5

1 Answers1

0

It's not at all clear what you are trying to do: perhaps you can clarify.

There are actually several ways of doing this. If the grid is set up with a DataTable as its source, then the easiest is probably to use the ItemsSource. This will be a DataTable and you can iterate over that as below. It looks like this was what you were trying to do:

DataTable a = ctrlgridviewdulieu0.ItemsSource as DataTable;
foreach (DataRow row in a.Rows)
{
    // The row is available: the itemArray is an array of column values
    object[] itemArray = row.ItemArray;
}

It's also possible in DevExpress grids to use native DevExpress functionality to iterate through rows. Their documentation is quite good on this, depending on your requirements.

Rich N
  • 8,939
  • 3
  • 26
  • 33
  • My grid use `Dataset` as `source`. I tried you way but it through this `exception`: `Object reference not set to an instance of an object` Sorry for repling late. – j.doen May 15 '18 at 09:37
  • A DataSet is a container for DataTables, so you need to work out where your data actually is. If you want the first DataTable in the Tables collection you can do: DataSet ds = ctrlgridviewdulieu0.ItemsSource as DataSet; DataTable a = ds.Tables[0]; and then use a as above. Try that. – Rich N May 15 '18 at 16:29
  • I tried but I always through this exception: `An exception of type 'System.NullReferenceException' occurred in WpfApplication5.exe but was not handled in user code `. I think somethings relate about `Handle` – j.doen May 16 '18 at 03:07
  • Try doing some basic debugging: put a breakpoint on the statement that accesses the ItemsSource and see if it has a value and what it's datatype is. If you're using Visual Studio you can do this by just hovering over it, or using the immediate window and typing it in. Once you know the datatype you can handle it appropriately. You haven't given enough information for anyone to help you here really. – Rich N May 17 '18 at 17:58
  • Greatly. I relized that `ItemSource` was `Data.Dataview`. And then I using loop to get all data of this `Dataview` but I failed ,I tried convert `row.Tostring()` . `DataView a = ctrlgridviewdulieu0.ItemsSource as DataView; foreach (DataRowView row in a)` – j.doen May 18 '18 at 01:35
  • https://stackoverflow.com/questions/1427840/looping-through-rows-in-a-dataview/1427841 – Rich N May 18 '18 at 12:44