c#中基于gridcontrol mainview的打印预览

print preview based on gridcontrol mainview in c#

如何在基于mainview的GridControl DevExpress中打印预览。
这是我的代码:

private void PrintPreview()
{            
    // Get your currently selected grid row
    var rowHandle = gridView1.FocusedRowHandle;

    // Get the value for the given column - convert to the type you're expecting
    var obj = gridView1.GetRowCellValue(rowHandle, "NIK");

    try
    {
        if (gridView1.RowCount <=0)

        {
            MessageBox.Show("DATA ROW KOSONG", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            OracleDataAdapter adp = new OracleDataAdapter();
            for (int i = 0; i < gridView1.DataRowCount; i++)
            {                        
                //connection
                if (koneksi.koneksi.con.State == ConnectionState.Open)
                {
                    koneksi.koneksi.con.Close();
                }
                koneksi.koneksi.con.Open();

                tambah buka = new tambah();

                OracleCommand cmd = new OracleCommand();
                cmd.Connection = koneksi.koneksi.con;
                adp = new OracleDataAdapter(@"SELECT * FROM V_KARYAWAN, KARYAWAN_GAMBAR WHERE V_KARYAWAN.NIK = KARYAWAN_GAMBAR.KARYAWAN_FK AND V_KARYAWAN.NIK = '" + obj + "'", koneksi.koneksi.con);
            }
            DataSet ds = new DataSet();
            adp.Fill(ds, "V_KARYAWAN, KARYAWAN_GAMBAR");
            kartu report = new kartu();
            report.DataSource = ds.Tables[0];
            report.ShowPreview();                                                               
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    } 
}

我很难解决这个问题。我无法从 GridControl mainview 获取值,因为我的代码只知道 GridControl.

中的 FocusedRowHandle
// Get your currently selected grid row
 var rowHandle = gridView1.FocusedRowHandle;

// Get the value for the given column - convert to the type you're expecting
 var obj = gridView1.GetRowCellValue(rowHandle, "NIK");

我使用的是 oracle 数据库。

您可以使用 GridControl.ShowPrintPreview method or GridControl.ShowRibbonPrintPreview 方法来显示您的 GridControl 的打印预览。

但是如果你想使用你的 XtraReport 那么你就差不多了。您只需要将用于获取值的代码移动到您的循环中。
这是示例:

private void PrintPreview()
{
    try
    {
        if (gridView1.RowCount <= 0)
        {
            MessageBox.Show("DATA ROW KOSONG", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            var table = new DataTable("V_KARYAWAN, KARYAWAN_GAMBAR");

            //Create columns in table form gridView1.
            foreach (GridColumn column in gridView1.Columns)
                table.Columns.Add(column.FieldName, column.ColumnType);

            //Export gridView1 to table.
            for (int rowHandle = 0; rowHandle < gridView1.DataRowCount; rowHandle++)
            {
                var row = table.NewRow();

                foreach (DataColumn column in table.Columns)
                    row[column] = gridView1.GetRowCellValue(rowHandle, column.ColumnName);

                table.Rows.Add(row);
            }

            //Show report.
            DataSet ds = new DataSet();
            ds.Tables.Add(table);

            kartu report = new kartu();
            report.DataSource = ds.Tables[0];
            report.ShowPreview();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}