 Main Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace TestAsync
{
public partial class SQLMain : Form
{
SQLOrder[] orderForm = new SQLOrder[GlobalForms.TotalTables];
DataTable emptyDT = null;
public static DataTable tempDT;
string strCon = System.Configuration.ConfigurationSettings.AppSettings["connString2"].ToString();
SqlConnection conn;
DataGridViewPrinter MyDataGridViewPrinter;
public SQLMain()
{
InitializeComponent();
this.dgvListOrder.CellContentClick += new DataGridViewCellEventHandler(dgvListOrder_CellContentClick);
dgvListOrder.Rows.Clear();
dgvListOrder.Columns.Clear();
DataGridViewButtonColumn btnTable = new DataGridViewButtonColumn();
btnTable.HeaderText = "Table";
btnTable.Name = "btnTable";
btnTable.Text = "Table";
dgvListOrder.Columns.Add(btnTable);
DataGridViewTextBoxColumn txtTotal = new DataGridViewTextBoxColumn();
txtTotal.HeaderText = "Item";
txtTotal.Name = "Item";
dgvListOrder.Columns.Add(txtTotal);
DataGridViewButtonColumn btnUpdate = new DataGridViewButtonColumn();
btnUpdate.HeaderText = "Update";
btnUpdate.Name = "btnUpdate";
btnUpdate.Text = "Table";
dgvListOrder.Columns.Add(btnUpdate);
for (int i = 0; i < GlobalForms.TotalTables; i++)
{
string[] row = new string[] { "Table" + (i + 1), "TotalPrice", "Update" };
dgvListOrder.Rows.Add(row);
orderForm[i] = new SQLOrder();
}
dgvListOrder.Columns[1].ReadOnly = true;
}
private void dgvListOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
lblTotal.Text = "0";
if (e.RowIndex > -1 && e.ColumnIndex == 0)
{
int tblNo = e.RowIndex + 1;
GlobalForms.strTableNo = "Table-" + tblNo;
GlobalForms.selectedTable = tblNo;
orderForm[e.RowIndex].ShowDialog();
fillGrid();
calculateTotal();
}
if (e.RowIndex > -1 && e.ColumnIndex == 1)
{
GlobalForms.selectedTable = e.RowIndex + 1;
fillGrid();
}
}
private void fillGrid()
{
conn = new SqlConnection(strCon);
SqlDataAdapter da;
DataTable dt = new DataTable();
string strCommand;
strCommand = "select Item, Price from zTable" + GlobalForms.selectedTable;
conn.Open();
da = new SqlDataAdapter(strCommand, conn);
da.Fill(dt);
dgvOrderDetails.DataSource = dt;
conn.Close();
calculateTotal();
}
private void calculateTotal()
{
double sum = 0;
foreach (DataGridViewRow row in dgvOrderDetails.Rows)
{
sum += Convert.ToDouble(row.Cells[1].Value);
}
lblTotal.Text = sum.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
conn = new SqlConnection(strCon);
string[] strCommand = new string[GlobalForms.TotalTables];
for (int i = 0; i < GlobalForms.TotalTables; i++)
{
strCommand[i] = "create table zTable" + (i + 1) +
" (ID int IDENTITY(1,1) NOT NULL, Item varchar(50), Price numeric(18,2))";
conn.Open();
SqlCommand cmd = new SqlCommand(strCommand[i], conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
conn = new SqlConnection(strCon);
string[] strCommand = new string[GlobalForms.TotalTables];
for (int i = 0; i < GlobalForms.TotalTables; i++)
{
strCommand[i] = "drop table zTable" + (i + 1);
conn.Open();
SqlCommand cmd = new SqlCommand(strCommand[i], conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
private void SQLMain_Load(object sender, EventArgs e)
{
dgvOrderDetails.DataSource = null;
}
private void btnCheckOut_Click(object sender, EventArgs e)
{
conn = new SqlConnection(strCon);
string strCommand;
strCommand = "truncate table zTable" + GlobalForms.selectedTable;
conn.Open();
SqlCommand cmd = new SqlCommand(strCommand, conn);
cmd.ExecuteNonQuery();
conn.Close();
int tempT = GlobalForms.selectedTable - 1;
orderForm[tempT].dgvOrderList.DataSource = emptyDT;
dgvOrderDetails.DataSource = null;
}
private void SQLMain_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
conn = new SqlConnection(strCon);
string[] strCommand = new string[GlobalForms.TotalTables];
for (int i = 0; i < GlobalForms.TotalTables; i++)
{
strCommand[i] = "drop table zTable" + (i + 1);
conn.Open();
SqlCommand cmd = new SqlCommand(strCommand[i], conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch
{ }
}
private bool SetupThePrinting()
{
PrintDialog MyPrintDialog = new PrintDialog();
MyPrintDialog.AllowCurrentPage = false;
MyPrintDialog.AllowPrintToFile = false;
MyPrintDialog.AllowSelection = false;
MyPrintDialog.AllowSomePages = false;
MyPrintDialog.PrintToFile = false;
MyPrintDialog.ShowHelp = false;
MyPrintDialog.ShowNetwork = false;
if (MyPrintDialog.ShowDialog() != DialogResult.OK)
return false;
MyPrintDocument.DocumentName = "Bill Print";
MyPrintDocument.PrinterSettings = MyPrintDialog.PrinterSettings;
MyPrintDocument.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings;
MyPrintDocument.DefaultPageSettings.Margins = new Margins(40, 40, 40, 40);
if (MessageBox.Show("Do you want the report to be centered on the page",
"BillManager - Center on Page", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
MyDataGridViewPrinter = new DataGridViewPrinter(dgvOrderDetails,
MyPrintDocument, true, true, "Bill", new Font("Tahoma", 18,
FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);
else
MyDataGridViewPrinter = new DataGridViewPrinter(dgvOrderDetails,
MyPrintDocument, false, true, "Bill", new Font("Tahoma", 18,
FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);
return true;
}
private void btnPrintPreview_Click(object sender, EventArgs e)
{
if (SetupThePrinting())
{
PrintPreviewDialog MyPrintPreviewDialog = new PrintPreviewDialog();
MyPrintPreviewDialog.Document = MyPrintDocument;
MyPrintPreviewDialog.ShowDialog();
}
}
private void MyPrintDocument_PrintPage_1(object sender, PrintPageEventArgs e)
{
bool more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics);
if (more == true)
e.HasMorePages = true;
}
}
}
Order Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;
namespace TestAsync
{
public partial class SQLOrder : Form
{
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
string strCon = System.Configuration.ConfigurationSettings.AppSettings["connString2"].ToString();
SqlDataReader dr;
SqlConnection conn;
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public SQLOrder()
{
InitializeComponent();
this.txtItems.KeyDown += new KeyEventHandler(txtItems_KeyDown);
conn = new SqlConnection(strCon);
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select distinct ItemName from zItemMaster order by ItemName";
conn.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
while (dr.Read())
{
namesCollection.Add(dr["ItemName"].ToString());
}
}
else
{
MessageBox.Show("Data not found");
}
dr.Close();
conn.Close();
txtItems.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtItems.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtItems.AutoCompleteCustomSource = namesCollection;
}
private void txtItems_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
retPrice();
btnAdd.Focus();
}
}
private bool IsitemExists()
{
int flagFound = 0;
if (dgvOrderList.Rows.Count == 0)
return false;
if (dgvOrderList.Rows.Count >= 1)
{
for (int i = 0; i < (dgvOrderList.Rows.Count); i++)
{
if (dgvOrderList.Rows[i].Cells["Item"].Value.ToString().ToUpper().Equals(txtItems.Text.ToUpper()))
{
flagFound++;
}
}
}
if (flagFound > 0)
return true;
else
return false;
}
private void retPrice()
{
SqlDataReader dr1;
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select ItemPrice from zItemMaster where ItemName = '" + txtItems.Text + "'";
conn.Open();
dr1 = cmd.ExecuteReader();
if (dr1.HasRows == true)
{
while (dr1.Read())
{
lblCurrentPrice.Text = dr1["ItemPrice"].ToString();
}
}
dr1.Close();
conn.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (txtItems.Text != "")
{
if (lblCurrentPrice.Text != "" || lblCurrentPrice.Text != "0")
{
if (txtQuantity.Text == "")
{
if (IsitemExists() == false)
{
string[] row = new string[] { txtItems.Text, lblCurrentPrice.Text };
dgvOrderList.Rows.Add(row);
conn = new SqlConnection(strCon);
string strCommand;
strCommand = "insert into zTable" + GlobalForms.selectedTable + " (Item, Price)" +
" values ('" + row[0] + "', " + row[1] + ")";
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strCommand;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.Close();
lblTotalPrice.Text = "";
calculateTotal();
txtItems.Clear();
lblCurrentPrice.Text = "";
txtItems.Focus();
}
else
{
MessageBox.Show("Item already in list.");
txtItems.Focus();
return;
}
}
}
if (txtQuantity.Text != "")
{
if (IsitemExists() == false)
{
double totPrice = Convert.ToDouble(Convert.ToDouble(lblCurrentPrice.Text) * Convert.ToDouble(txtQuantity.Text));
string[] row = new string[] { txtItems.Text, totPrice.ToString() };
dgvOrderList.Rows.Add(row);
conn = new SqlConnection(strCon);
string strCommand;
strCommand = "insert into zTable" + GlobalForms.selectedTable + " (Item, Price)" +
" values ('" + row[0] + "', " + row[1] + ")";
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strCommand;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.Close();
lblTotalPrice.Text = "";
calculateTotal();
txtItems.Clear();
lblCurrentPrice.Text = "";
txtQuantity.Clear();
txtItems.Focus();
}
}
else
{
MessageBox.Show("Item already in list.");
txtItems.Focus();
return;
}
}
else
{
MessageBox.Show("Please Select the item first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtItems.Focus();
}
}
private void calculateTotal()
{
double sum = 0;
foreach (DataGridViewRow row in dgvOrderList.Rows)
{
sum += Convert.ToDouble(row.Cells[1].Value);
}
lblTotalPrice.Text = sum.ToString();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Hide();
}
private void SQLOrder_Load(object sender, EventArgs e)
{
this.Text = GlobalForms.strTableNo;
}
private void btnCheckOut_Click(object sender, EventArgs e)
{
try
{
DataTable emptyDT = new DataTable();
conn = new SqlConnection(strCon);
string strCommand;
strCommand = "truncate table zTable" + GlobalForms.selectedTable;
conn.Open();
SqlCommand cmd = new SqlCommand(strCommand, conn);
cmd.ExecuteNonQuery();
conn.Close();
emptyDT.Clear();
this.dgvOrderList.DataSource = emptyDT;
this.dgvOrderList.DataSource = null;
this.Close();
}
catch
{ }
}
private void dgvOrderList_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
conn = new SqlConnection(strCon);
string strCommand;
strCommand = "DELETE FROM zTable" + GlobalForms.selectedTable +
" WHERE Item = '" + dgvOrderList.Rows[e.RowIndex].Cells[0].Value + "'";
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strCommand;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.Close();
dgvOrderList.Rows.RemoveAt(e.RowIndex);
calculateTotal();
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
for (int j = 0; j < (dgvOrderList.Rows.Count); j++)
{
dgvOrderList.Rows[j].Selected = false;
}
for (int i = 0; i < (dgvOrderList.Rows.Count); i++)
{
if (dgvOrderList.Rows[i].Cells["Item"].Value.ToString().StartsWith(txtItems.Text, true, CultureInfo.InvariantCulture))
{
dgvOrderList.Rows[i].DefaultCellStyle.BackColor = Color.LightPink;
}
else
dgvOrderList.Rows[i].DefaultCellStyle.BackColor = Color.White;
}
}
}
}
MGlobal Class
public static class GlobalForms
{
public static OrderForm ordfrm;
public static string strTableNo;
public static int selectedTable = 0;
public static int TotalTables = 5;
}
Printer Class .cs
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
class DataGridViewPrinter
{
private DataGridView TheDataGridView;
private PrintDocument ThePrintDocument;
private bool IsCenterOnPage;
private bool IsWithTitle;
private string TheTitleText;
private Font TheTitleFont;
private Color TheTitleColor;
private bool IsWithPaging;
static int CurrentRow;
static int PageNumber;
private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;
private float CurrentY;
private float RowHeaderHeight;
private List<float> RowsHeight;
private List<float> ColumnsWidth;
private float TheDataGridViewWidth;
private List<int[]> mColumnPoints;
private List<float> mColumnPointsWidth;
private int mColumnPoint;
public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument, bool CenterOnPage, bool WithTitle, string aTitleText, Font aTitleFont, Color aTitleColor, bool WithPaging)
{
TheDataGridView = aDataGridView;
ThePrintDocument = aPrintDocument;
IsCenterOnPage = CenterOnPage;
IsWithTitle = WithTitle;
TheTitleText = aTitleText;
TheTitleFont = aTitleFont;
TheTitleColor = aTitleColor;
IsWithPaging = WithPaging;
PageNumber = 0;
RowsHeight = new List<float>();
ColumnsWidth = new List<float>();
mColumnPoints = new List<int[]>();
mColumnPointsWidth = new List<float>();
if (!ThePrintDocument.DefaultPageSettings.Landscape)
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
else
{
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;
CurrentRow = 0;
}
private void Calculate(Graphics g)
{
if (PageNumber == 0)
{
SizeF tmpSize = new SizeF();
Font tmpFont;
float tmpWidth;
TheDataGridViewWidth = 0;
for (int i = 0; i < TheDataGridView.Columns.Count; i++)
{
tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font;
if (tmpFont == null)
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString(TheDataGridView.Columns[i].HeaderText, tmpFont);
tmpWidth = tmpSize.Width;
RowHeaderHeight = tmpSize.Height;
for (int j = 0; j < TheDataGridView.Rows.Count; j++)
{
tmpFont = TheDataGridView.Rows[j].DefaultCellStyle.Font;
if (tmpFont == null)
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString("Anything", tmpFont);
RowsHeight.Add(tmpSize.Height);
tmpSize = g.MeasureString(TheDataGridView.Rows[j].Cells[i].EditedFormattedValue.ToString(), tmpFont);
if (tmpSize.Width > tmpWidth)
tmpWidth = tmpSize.Width;
}
if (TheDataGridView.Columns[i].Visible)
TheDataGridViewWidth += tmpWidth;
ColumnsWidth.Add(tmpWidth);
}
int k;
int mStartPoint = 0;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
if (TheDataGridView.Columns[k].Visible)
{
mStartPoint = k;
break;
}
int mEndPoint = TheDataGridView.Columns.Count;
for (k = TheDataGridView.Columns.Count - 1; k >= 0; k--)
if (TheDataGridView.Columns[k].Visible)
{
mEndPoint = k + 1;
break;
}
float mTempWidth = TheDataGridViewWidth;
float mTempPrintArea = (float)PageWidth - (float)LeftMargin - (float)RightMargin;
if (TheDataGridViewWidth > mTempPrintArea)
{
mTempWidth = 0.0F;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
{
if (TheDataGridView.Columns[k].Visible)
{
mTempWidth += ColumnsWidth[k];
if (mTempWidth > mTempPrintArea)
{
mTempWidth -= ColumnsWidth[k];
mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
mColumnPointsWidth.Add(mTempWidth);
mStartPoint = k;
mTempWidth = ColumnsWidth[k];
}
}
mEndPoint = k + 1;
}
}
mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
mColumnPointsWidth.Add(mTempWidth);
mColumnPoint = 0;
}
}
private void DrawHeader(Graphics g)
{
CurrentY = (float)TopMargin;
if (IsWithPaging)
{
PageNumber++;
string PageString = "Page " + PageNumber.ToString();
StringFormat PageStringFormat = new StringFormat();
PageStringFormat.Trimming = StringTrimming.Word;
PageStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
PageStringFormat.Alignment = StringAlignment.Far;
Font PageStringFont = new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);
RectangleF PageStringRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(PageString, PageStringFont).Height);
g.DrawString(PageString, PageStringFont, new SolidBrush(Color.Black), PageStringRectangle, PageStringFormat);
CurrentY += g.MeasureString(PageString, PageStringFont).Height;
}
if (IsWithTitle)
{
StringFormat TitleFormat = new StringFormat();
TitleFormat.Trimming = StringTrimming.Word;
TitleFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
if (IsCenterOnPage)
TitleFormat.Alignment = StringAlignment.Center;
else
TitleFormat.Alignment = StringAlignment.Near;
RectangleF TitleRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(TheTitleText, TheTitleFont).Height);
g.DrawString(TheTitleText, TheTitleFont, new SolidBrush(TheTitleColor), TitleRectangle, TitleFormat);
CurrentY += g.MeasureString(TheTitleText, TheTitleFont).Height;
}
float CurrentX = (float)LeftMargin;
if (IsCenterOnPage)
CurrentX += (((float)PageWidth - (float)RightMargin - (float)LeftMargin) - mColumnPointsWidth[mColumnPoint]) / 2.0F;
Color HeaderForeColor = TheDataGridView.ColumnHeadersDefaultCellStyle.ForeColor;
if (HeaderForeColor.IsEmpty)
HeaderForeColor = TheDataGridView.DefaultCellStyle.ForeColor;
SolidBrush HeaderForeBrush = new SolidBrush(HeaderForeColor);
Color HeaderBackColor = Color.White;
if (HeaderBackColor.IsEmpty)
HeaderBackColor = TheDataGridView.DefaultCellStyle.BackColor;
SolidBrush HeaderBackBrush = new SolidBrush(HeaderBackColor);
Pen TheLinePen = new Pen(Color.White);
Font HeaderFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font;
if (HeaderFont == null)
HeaderFont = TheDataGridView.DefaultCellStyle.Font;
RectangleF HeaderBounds = new RectangleF(CurrentX, CurrentY, mColumnPointsWidth[mColumnPoint], RowHeaderHeight);
g.FillRectangle(HeaderBackBrush, HeaderBounds);
StringFormat CellFormat = new StringFormat();
CellFormat.Trimming = StringTrimming.Word;
CellFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
RectangleF CellBounds;
float ColumnWidth;
for (int i = (int)mColumnPoints[mColumnPoint].GetValue(0); i < (int)mColumnPoints[mColumnPoint].GetValue(1); i++)
{
if (!TheDataGridView.Columns[i].Visible) continue;
ColumnWidth = ColumnsWidth[i];
if (TheDataGridView.ColumnHeadersDefaultCellStyle.Alignment.ToString().Contains("Right"))
CellFormat.Alignment = StringAlignment.Far;
else if (TheDataGridView.ColumnHeadersDefaultCellStyle.Alignment.ToString().Contains("Center"))
CellFormat.Alignment = StringAlignment.Center;
else
CellFormat.Alignment = StringAlignment.Near;
CellBounds = new RectangleF(CurrentX, CurrentY, ColumnWidth, RowHeaderHeight);
g.DrawString(TheDataGridView.Columns[i].HeaderText, HeaderFont, HeaderForeBrush, CellBounds, CellFormat);
if (TheDataGridView.RowHeadersBorderStyle != DataGridViewHeaderBorderStyle.None)
g.DrawRectangle(TheLinePen, CurrentX, CurrentY, ColumnWidth, RowHeaderHeight);
CurrentX += ColumnWidth;
}
CurrentY += RowHeaderHeight + 10;
}
private bool DrawRows(Graphics g)
{
Pen TheLinePen = new Pen(Color.White);
Font RowFont;
Color RowForeColor;
Color RowBackColor;
SolidBrush RowForeBrush;
SolidBrush RowBackBrush;
SolidBrush RowAlternatingBackBrush;
StringFormat CellFormat = new StringFormat();
CellFormat.Trimming = StringTrimming.Word;
CellFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;
RectangleF RowBounds;
float CurrentX;
float ColumnWidth;
while (CurrentRow < TheDataGridView.Rows.Count)
{
if (TheDataGridView.Rows[CurrentRow].Visible)
{
RowFont = TheDataGridView.Rows[CurrentRow].DefaultCellStyle.Font;
if (RowFont == null)
RowFont = TheDataGridView.DefaultCellStyle.Font;
RowForeColor = TheDataGridView.Rows[CurrentRow].DefaultCellStyle.ForeColor;
if (RowForeColor.IsEmpty)
RowForeColor = TheDataGridView.DefaultCellStyle.ForeColor;
RowForeBrush = new SolidBrush(RowForeColor);
RowBackColor = TheDataGridView.Rows[CurrentRow].DefaultCellStyle.BackColor;
if (RowBackColor.IsEmpty)
{
RowBackBrush = new SolidBrush(TheDataGridView.DefaultCellStyle.BackColor);
RowAlternatingBackBrush = new SolidBrush(TheDataGridView.AlternatingRowsDefaultCellStyle.BackColor);
}
else
{
RowBackBrush = new SolidBrush(RowBackColor);
RowAlternatingBackBrush = new SolidBrush(RowBackColor);
}
CurrentX = (float)LeftMargin;
if (IsCenterOnPage)
CurrentX += (((float)PageWidth - (float)RightMargin - (float)LeftMargin) - mColumnPointsWidth[mColumnPoint]) / 2.0F;
RowBounds = new RectangleF(CurrentX, CurrentY, mColumnPointsWidth[mColumnPoint], RowsHeight[CurrentRow]);
if (CurrentRow % 2 == 0)
g.FillRectangle(RowBackBrush, RowBounds);
else
g.FillRectangle(RowAlternatingBackBrush, RowBounds);
for (int CurrentCell = (int)mColumnPoints[mColumnPoint].GetValue(0); CurrentCell < (int)mColumnPoints[mColumnPoint].GetValue(1); CurrentCell++)
{
if (!TheDataGridView.Columns[CurrentCell].Visible) continue;
if (TheDataGridView.Columns[CurrentCell].DefaultCellStyle.Alignment.ToString().Contains("Right"))
CellFormat.Alignment = StringAlignment.Far;
else if (TheDataGridView.Columns[CurrentCell].DefaultCellStyle.Alignment.ToString().Contains("Center"))
CellFormat.Alignment = StringAlignment.Center;
else
CellFormat.Alignment = StringAlignment.Near;
ColumnWidth = ColumnsWidth[CurrentCell];
RectangleF CellBounds = new RectangleF(CurrentX, CurrentY, ColumnWidth, RowsHeight[CurrentRow]);
g.DrawString(TheDataGridView.Rows[CurrentRow].Cells[CurrentCell].EditedFormattedValue.ToString(), RowFont, RowForeBrush, CellBounds, CellFormat);
if (TheDataGridView.CellBorderStyle != DataGridViewCellBorderStyle.None)
g.DrawRectangle(TheLinePen, CurrentX, CurrentY, ColumnWidth, RowsHeight[CurrentRow]);
CurrentX += ColumnWidth;
}
CurrentY += RowsHeight[CurrentRow];
if ((int)CurrentY > (PageHeight - TopMargin - BottomMargin))
{
CurrentRow++;
return true;
}
}
CurrentRow++;
}
CurrentRow = 0;
mColumnPoint++;
if (mColumnPoint == mColumnPoints.Count)
{
mColumnPoint = 0;
return false;
}
else
return true;
}
public bool DrawDataGridView(Graphics g)
{
try
{
Calculate(g);
DrawHeader(g);
bool bContinue = DrawRows(g);
return bContinue;
}
catch (Exception ex)
{
MessageBox.Show("Operation failed: " + ex.Message.ToString(), Application.ProductName + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
|