简体   繁体   English

如何一键获取gridcontrol中的所有数据

[英]How to get all data in gridcontrol by one click

How to use loop to read all records by one click on the button . 如何通过单击button使用循环读取所有记录。 I have to print many reports.For each row in the table I need to create a report. 我必须打印许多报告。我需要为表中的每一行创建一个报告。 And read until the last row of the table . 并阅读直到表的最后一行。 My idea is using loop or index table but i don't know how to do it. 我的想法是使用循环或索引表,但我不知道该怎么做。 This is my code: 这是我的代码:

private void btnin_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand(" SELECT * FROM viewdata1 WHERE Customers = '" + cbbcustomer.Text + "'", cnn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cnn.Close();

            XtraReport1 report = new XtraReport1();
            report.DataSource = dt;
            report.ShowPreviewDialog();
        }
        catch ( Exception ex )
        {
            MessageBox.Show(ex.Message);
        }

    }

You could iterate through the Datatable 您可以遍历数据表

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

    dtrow.ImportRow(row);
    XtraReport1 report = new XtraReport1();
    report.DataSource = dtrow;
    //report.ShowPreviewDialog();  Not sure what happens here but maybe a print method is better suited?        
}

Basically for each row you create a datatable with the same structure and import one row. 基本上,您将为每一行创建具有相同结构的数据表并导入一行。 Then its assigned as your datasource. 然后将其分配为您的数据源。 This will iterate through all rows. 这将遍历所有行。

For some reasons, I had to change something in my code. 由于某些原因,我不得不更改代码。 I used Mark Vance's code but it didn't work: 我使用了Mark Vance's code但没有用:

        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.ToString() + "'", 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);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM