0

How can I printpreview in my GridControl DevExpress based on mainview.
Here is my code:

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);
    } 
}

It was difficult for me how to solve this. I cannot take the value from GridControl mainview because mycode just know about FocusedRowHandle in GridControl.

// 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");

I was using oracle database.

nempoBu4
  • 6,521
  • 8
  • 35
  • 40
aminvincent
  • 553
  • 1
  • 12
  • 43
  • Have you tried [`GridControl.ShowPrintPreview`](https://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridGridControl_ShowPrintPreviewtopic) method or [`GridControl.ShowRibbonPrintPreview`](https://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridGridControl_ShowRibbonPrintPreviewtopic) method? – nempoBu4 Apr 13 '15 at 04:20
  • no, i haven't to do that,..because i use xtrareport to show data from gridcontrol,..but i'll try first use GridControl.ShowPreview... – aminvincent Apr 13 '15 at 06:01

1 Answers1

0

You can use GridControl.ShowPrintPreview method or GridControl.ShowRibbonPrintPreview method to show the printpreview of your GridControl.

But if you want to use your XtraReport then you are almost there. You just need to move your code for getting values into your cycle.
Here is example:

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);
    }
}
nempoBu4
  • 6,521
  • 8
  • 35
  • 40
  • i have tried for your suggest code ,..but it just show first data in gridcontrol row but another row wasn't ,...i dont't know how to solve this,...perhaps i got the wrong code in for loop ? – aminvincent Apr 13 '15 at 08:35
  • @aminvincent It is hard to say what is wrong in your code, because I have no information what have you done and what is the structure of your underline datasource in your `GridControl`. Can you add this information into your question? – nempoBu4 Apr 13 '15 at 09:05
  • for example i have 4 rows data show in gridcontrol and 4 columns,..mycolumn (NIK,NAME,ADDRESS,PHONE NUMBER) and my 4 rows data are (1, VINCENT, JAKARTA, 1234), (2, FIAN, KUDUS, 567), (3, DIANA, JEPARA, 90987), (4, VIA, SEMARANG, 6778) . i wanna show that data in xtrareport based on gridcontrol devespress. but your suggest code above just show first data row in my xtrareport,....how can i solve this? – aminvincent Apr 14 '15 at 03:43
  • @aminvincent You can directly export your `GridView` to `DataTable` and use the result for your `XtraReport`. I have updated my answer. Take a look at the code. – nempoBu4 Apr 14 '15 at 03:53
  • after i try n erorr for my project,.. and i use your code above,..it's work,..my problem is because of my xtrareport databinding,.. thanks so much for your help....nice... – aminvincent Apr 14 '15 at 04:31