Thursday, July 7, 2011

make a XtraGrid cell read-only on condition

using System;
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing;  
using System.Text; 
using System.Windows.Forms; 
using DevExpress.XtraGrid.Views.Grid; 
namespace ReturnForm
 {
      public partial class Form1 : Form {
          public ReturnForm()   
          {
              InitializeComponent();
          }
          private void Form1_Load(object sender, EventArgs e)
          {
              FillDataSource();
          } 
          private void FillDataSource()
          {
              dtProducts = dsProducts.dtProductsTableAdapter.Fill( 
                                                this.dsProducts.dtProducts);
          }
          private bool IsShipToUSCanada(GridView view, int row)
          {
              try
            {
                    string val = Convert.ToString(
                                view.GetRowCellValue(row, "ShipCountry"));
                  return (val == "US" || val == "Canada");
              }
             catch( )
            {
                  return false;
            }
      }
         private void grvProducts_ShowingEditor(object sender, CancelEventArgs e)
        {
              if(gridView1.FocusedColumn.FieldName == "IsFreeShipping" 
                 && IsShipToUSCanada(grvProducts, grvProducts.FocusedRowHandle))
                  e.Cancel = true;
       }
        private void grvProducts_RowCellStyle(object sender,
                      DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        { 
             if(e.Column.FieldName == "IsFreeShipping" 
                               && IsShipToUSCanada(grvProducts, e.RowHandle))
            {
                  e.Appearance.BackColor = Color.LightGray; 
             } 
         }
      }
  }

No comments :

Post a Comment