PDA

View Full Version : امکان تغییر خاصیت Enable یک دکمه (Button) در دیتاگریدویو؟



sajad_3dmax
پنج شنبه 04 تیر 1394, 11:31 صبح
سلام دوستان. بنده یکی از ستون های دیتا گرید ویو ام رو بصورت Button تعریف کردم برا ثبت نهایی. امکانش هست که وقتی کاربر یکبار روی این دکمه در دیتاگریدویو کلیک میکنه خاصیت Enable این دکمه رو تغییر داد.
اصلا دکمه هایی که روی دیتاگرید ویو قرار میگیرند خاصیت Enable دارند؟

Mahmoud.Afrad
پنج شنبه 04 تیر 1394, 13:22 عصر
دو کلاس زیر رو به پروژه اضافه کن. یکبار پروژه رو build کن.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
{
public DataGridViewDisableButtonColumn()
{
this.CellTemplate = new DataGridViewDisableButtonCell();
}
}

public class DataGridViewDisableButtonCell : DataGridViewButtonCell
{
private bool enabledValue;

public bool Enabled
{
get { return enabledValue; }
set { enabledValue = value; }
}

// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
DataGridViewDisableButtonCell cell =
(DataGridViewDisableButtonCell) base.Clone();
cell.Enabled = this.Enabled;
return cell;
}

// By default, enable the button cell.
public DataGridViewDisableButtonCell()
{
this.enabledValue = true;
}

protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value,
object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// The button cell is disabled, so paint the border,
// background, and disabled button for the cell.
if (!this.enabledValue)
{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}

// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}

// Calculate the area in which to draw the button.
Rectangle buttonArea = cellBounds;
Rectangle buttonAdjustment =
this.BorderWidths(advancedBorderStyle);
buttonArea.X += buttonAdjustment.X;
buttonArea.Y += buttonAdjustment.Y;
buttonArea.Height -= buttonAdjustment.Height;
buttonArea.Width -= buttonAdjustment.Width;

// Draw the disabled button.
ButtonRenderer.DrawButton(graphics, buttonArea,
PushButtonState.Disabled);

// Draw the disabled button text.
if (this.FormattedValue is String)
{
TextRenderer.DrawText(graphics,
(string) this.FormattedValue,
this.DataGridView.Font,
buttonArea, SystemColors.GrayText);
}
}
else
{
// The button cell is enabled, so let the base class
// handle the painting.
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
}

حالا ستونی از نوع DataGridViewDisableButtonColumn به دیتاگرید اضافه کن و یا نوع ستونی که قبلا ایجاد کردی رو از DataGridViewButtonColumn به DataGridViewDisableButtonColumn تغییر بده.
حالا در رویداد CellClick دیتاگرید به صورت زیر عمل کن. (btnColumn همون ستون از نوع button هست که مرحله قبل ایجاد شد)

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == btnColumn.Index)
{
DataGridView dgv = sender as DataGridView;
if (dgv == null) return;
DataGridViewDisableButtonCell dgvBtnCell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewDisableButtonCell;
if (dgvBtnCell != null && dgvBtnCell.Enabled)
{
// Your code here
// ...

dgvBtnCell.Enabled = false;
}
}
}


جایی که Your code here نوشته شده کدی که میخوای در هنگام کلیک اجرا بشه رو بنویس و بعد دکمه رو غیرفعال کن.



منبع:
https://msdn.microsoft.com/en-US/library/ms171619(v=vs.80).aspx