PDA

View Full Version : آموزش: Merge کردن سلول هایی از گریدویو که مقدار یکسان دارند ...



ghasem110deh
یک شنبه 13 اردیبهشت 1394, 23:52 عصر
سلام به همه ...
مرج کردن سلولهایی که مقدار مساوی دارن خیلی چیز مهمی نیست ولی چون خیلی دنبالش گشتم ، اینجا هم میزارم ! (عکس ضمیمه)
از یه سایت چینی گرفتم ! مواظب باشین سیستم نپوکه http://ashiyane.org/forums/images/smilies/smile.gif

--------------------------------------------------
اول این متودها رو ایجاد کنین :


bool IsTheSameCellValue(int column, int row)
{
DataGridViewCell cell1 = dataGridView1[column, row];
DataGridViewCell cell2 = dataGridView1[column, row - 1];
if (cell1.Value == null || cell2.Value == null)
{
return false;
}
if (cell1.Value.ToString() == cell2.Value.ToString())
{
return true;
}
else
{
return false;
}
}

private bool IsRepeatedCellValue(int p1, int p2)
{
throw new NotImplementedException();
}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == 0)
return;

if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.Value = "";
e.FormattingApplied = true;
}
}


حالا تو رویداد CellPainting این کد رو بنویسید :



private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) //
{
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.RowIndex < 1 || e.ColumnIndex < 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Top = dataGridView1.AdvancedCellBorderStyle.Top;
}



در آخر هم توی لود_فرم :


dataGridView1.AutoGenerateColumns = false;

ghasem110deh
شنبه 19 اردیبهشت 1394, 23:56 عصر
سلام به همه ...
در خدمت دوستان هستیم با رنگ بندیی دیتاگریدویو (سطرها)
-----------------------------------------------------------------
کافیه در رویداد CellPainting کدای زیر رو بنویسید :

if (e.ColumnIndex >= 0 && e.RowIndex >= 0 && (e.PaintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background) {
Color bColor1, bColor2;
if ((e.PaintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground &&
(e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
{
bColor1 = e.CellStyle.SelectionBackColor;
bColor2 = Color.DarkBlue;
}
else
{
bColor1 = e.CellStyle.BackColor;
bColor2 = Color.LemonChiffon;
}
using (System.Drawing.Drawing2D.LinearGradientBrush b = new System.Drawing.Drawing2D.LinearGradientBrush(e.Cel lBounds, bColor1, bColor2, System.Drawing.Drawing2D.LinearGradientMode.Horizo ntal))
{
e.Graphics.FillRectangle(b, e.CellBounds);
}
DataGridViewPaintParts paintParts = e.PaintParts & ~DataGridViewPaintParts.Background;
e.Paint(e.ClipBounds, paintParts);
e.Handled = true;
}

حالا دیتاگریدتون شبیه عکس زیر میشه :)