using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

using Stimulsoft.Report;
namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=dbTest;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("select * from tblPerson", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           if(dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor==SystemColors.Highlight)
           {
               dataGridView1.Rows[e.RowIndex].Selected = false;
               dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;

           }
            else
               dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = SystemColors.Highlight;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Code", typeof(string)));
            dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
            dt.Columns.Add(new DataColumn("LastName", typeof(string)));
            int dtRow = 0;
            for(int row=0;row<dataGridView1.Rows.Count;row++)
            {
                if (dataGridView1.Rows[row].DefaultCellStyle.BackColor == SystemColors.Highlight)
                {
                    dt.Rows.Add();
                    dt.Rows[dtRow][0] = dataGridView1.Rows[row].Cells[0].Value.ToString();
                    dt.Rows[dtRow][1] = dataGridView1.Rows[row].Cells[1].Value.ToString();
                    dt.Rows[dtRow][2] = dataGridView1.Rows[row].Cells[2].Value.ToString();
                    dtRow += 1;
                }
            }

            if(dt.Rows.Count>0)
            {
                StiReport report = new StiReport();
                report.Load(Application.StartupPath + "\\Report.mrt");
                report.RegBusinessObject("Person", dt);
                report.Show();
            }
        }
    }
}
