نمایش نتایج 1 تا 2 از 2

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

  1. #1
    کاربر دائمی آواتار sajad_3dmax
    تاریخ عضویت
    مرداد 1388
    محل زندگی
    قزوین _ زیباشهر
    پست
    296

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

    سلام دوستان. بنده یکی از ستون های دیتا گرید ویو ام رو بصورت Button تعریف کردم برا ثبت نهایی. امکانش هست که وقتی کاربر یکبار روی این دکمه در دیتاگریدویو کلیک میکنه خاصیت Enable این دکمه رو تغییر داد.
    اصلا دکمه هایی که روی دیتاگرید ویو قرار میگیرند خاصیت Enable دارند؟

  2. #2

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

    دو کلاس زیر رو به پروژه اضافه کن. یکبار پروژه رو 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/lib...(v=vs.80).aspx

تاپیک های مشابه

  1. سوال: تغییر خاصیت دکمه اینتر در GridView
    نوشته شده توسط perkas در بخش C#‎‎
    پاسخ: 3
    آخرین پست: جمعه 01 شهریور 1392, 15:38 عصر
  2. سوال: تغییر رنگ فونت تکست باکس در حالتی که خاصیت Enable=false باشه
    نوشته شده توسط pooya1072 در بخش VB.NET
    پاسخ: 3
    آخرین پست: پنج شنبه 15 تیر 1391, 02:13 صبح
  3. [b]امکان تغییر رنگ زمینه در Command Button و Tab Page ؟
    نوشته شده توسط Heydari865 در بخش Access
    پاسخ: 5
    آخرین پست: دوشنبه 06 تیر 1390, 07:26 صبح
  4. تغییر مکان یک دکمه button از طریق تغییر مختصات x,y در صفحه
    نوشته شده توسط tayebeh در بخش ASP.NET Web Forms
    پاسخ: 8
    آخرین پست: سه شنبه 27 بهمن 1383, 07:04 صبح
  5. باز کردن یک لیست از داده ها با زدن دکمه Button از datagrid
    نوشته شده توسط ahmad_rabie2002 در بخش VB.NET
    پاسخ: 1
    آخرین پست: یک شنبه 06 مهر 1382, 17:03 عصر

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •