2012-04-26 24 views
8

में विशेष चेक बॉक्स सेल को अक्षम कैसे करें मेरे पास DataGridView नियंत्रण के साथ WinForm है। इसमें 5 कॉलम हैं, उनमें से एक चेकबॉक्स कॉलम है। मैं एक ही पंक्ति में किसी अन्य कॉलम में मौजूद मान के आधार पर इस कॉलम के चेकबॉक्स सेल को सक्षम/अक्षम करना चाहता हूं।डेटाग्रिड व्यू चेकबॉक्स कॉलम

मैं DisabledCheckBoxCell

का उपयोग कर संपूर्ण स्तंभ निष्क्रिय कर सकते हैं लेकिन यह विकलांग राज्य में संपूर्ण स्तंभ बना देता है।

यहाँ DataGridView का एक टुकड़ा,

SourceColumn है | DestinationColumn
सच                                 | सक्षम
सच                                 | सक्षम
झूठी                               | विकलांग

किसी को भी विचार यह, कैसे नेट में प्राप्त किया जा सकता है।

उत्तर

0

अपने GridView के लिए RowDataBound घटना का उपयोग करें। आप घटनाओं को पंक्ति में डाल सकते हैं, इस पंक्ति पर नियंत्रण पा सकते हैं और इसे अक्षम कर सकते हैं। यह घटना ग्रिडव्यू की प्रत्येक पंक्ति के लिए भी हैडर, यहां तक ​​कि शीर्षलेख और पाद लेखों के लिए, इसलिए अपवाद को पकड़ने की कोशिश न करें। यहाँ कुछ कोड आपके लिए उपयोगी हो सकता है

protected void xxx_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if ((e.Row != null) && e.Row.RowType == DataControlRowType.DataRow) 
+0

मैं संबंधित पंक्ति में सेल को निष्क्रिय करना चाहते, मैं एक सेल पर संपत्ति सक्षम नहीं पा सके। मैं RowPostPaint घटना का उपयोग कर रहा हूँ। –

+0

इसे Winforms नहीं है? –

+0

मुझे डर लग रहा है कि वहाँ किसी एक सेल निष्क्रिय करने के लिए किसी भी समारोह नहीं हो सकता है, लेकिन आप कुछ देर के सेल क्लिक किया जाता है क्या कर सकते हैं, उदाहरण के सेल आप – Nick

20

विजय,

DataGridViewCheckBoxColumn संपत्ति विकलांग नहीं बुलाया है तो चेकबॉक्स की शैली को बदलने के द्वारा आप इसे पसंद के रूप में अगर वह अक्षम है देख सकते हैं। निम्नलिखित कोड को देखो।

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
    { 
     if (e.RowIndex == 1) 
     { 
      DataGridViewCell cell=dataGridView1.Rows[e.RowIndex].Cells[0]; 
      DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell; 
      chkCell.Value = false; 
      chkCell.FlatStyle = FlatStyle.Flat; 
      chkCell.Style.ForeColor = Color.DarkGray; 
      cell.ReadOnly = true; 

     } 

    } 
+0

आपके उत्तर के लिए धन्यवाद। यह कुछ मामूली मुद्दों के साथ काम कर रहा है।मेरे पास कॉलम हेडर में भी चेकबॉक्स कक्ष हैं। यह भी उन्हें प्रभावित कर रहा है। जब मैं अन्य ऐप पर स्विच करता हूं और मूल ऐप पर वापस जाता हूं, तो यूआई विकृत दिखता है। –

1

मैं दिखाने के लिए एक वास्तविक विकलांग चेकबॉक्स पाने के लिए कुछ इस तरह कर रही है समाप्त हो गया:

using System.Windows.Forms.VisualStyles; 

public partial class YourForm : Form 
{ 

    private static readonly VisualStyleRenderer DisabledCheckBoxRenderer; 

    static YourForm() 
    { 
     DisabledCheckBoxRenderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.UncheckedDisabled); 
    } 

    private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
    { 
     if (e.RowIndex > -1) 
     { 
      int checkBoxColumnIndex = this.yourCheckBoxColumn.Index; 
      var checkCell = (DataGridViewCheckBoxCell)this.dataGridView[checkBoxColumnIndex, e.RowIndex]; 
      var bounds = this.dataGridView.GetCellDisplayRectangle(checkBoxColumnIndex , e.RowIndex, false); 

      // i was drawing a disabled checkbox if i had set the cell to read only 
      if (checkCell.ReadOnly) 
      { 
       const int CheckBoxWidth = 16; 
       const int CheckBoxHeight = 16; 

       // not taking into consideration any cell style paddings 
       bounds.X += (bounds.Width - CheckBoxWidth)/2; 
       bounds.Y += (bounds.Height - CheckBoxHeight)/2; 
       bounds.Width = CheckBoxWidth; 
       bounds.Height = CheckBoxHeight; 

       if (VisualStyleRenderer.IsSupported) 
       { 

        // the typical way the checkbox will be drawn 
        DisabledCheckBoxRenderer.DrawBackground(e.Graphics, bounds); 
       } 
       else 
       { 

        // this method is only drawn if the visual styles of the application 
        // are turned off (this is for full support) 
        ControlPaint.DrawCheckBox(e.Graphics, bounds, ButtonState.Inactive); 
       } 
      } 
     } 
    } 
} 
संबंधित मुद्दे