Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 221769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:03:52+00:00 2026-05-11T19:03:52+00:00

i have System.Data.DataRows with several fields, most of them just plain types like int,

  • 0

i have System.Data.DataRows with several fields, most of them just plain types like int, single, string.

what is the best way to make them editable using a propertygrid?
it should work automatically no matter what kind of fields the datarow has, but it should not display all of them. i want to provide a list of properties that should be hidden.

since the DataTable is autogenerated i cannot add custom attributes like [Browsable(false)]

thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:03:53+00:00Added an answer on May 11, 2026 at 7:03 pm

    Edited to handle filtering; much tricker: in addition to getting the DataRowView, we need to provide a custom component that pretends (via pass-thru PropetyDescriptors) to be the DataRowView (which is itself pretending to be the DataRow) – and filter out the properties that we don’t want.

    Very interesting problem ;-p Easier to solve in classic classes, but the below works for DataRow ;-p

    Note that you could do other things in this area to make some of the properties non-editable (IsReadOnly), or have a different caption (DisplayName), or category (Category) – by overriding other members in RowWrapperDescriptor.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Windows.Forms;
    static class program
    {
        [STAThread]
        static void Main()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Foo", typeof(int));
            table.Columns.Add("Bar", typeof(string));
            table.Columns.Add("Audit", typeof(DateTime));
    
            table.Rows.Add(1, 14, "abc", DateTime.MinValue);
            DataRow row = table.Rows.Add(2,13,"def", DateTime.MinValue);
            table.Rows.Add(3, 24, "ghi", DateTime.MinValue);
    
            RowWrapper wrapper = new RowWrapper(row);
            wrapper.Exclude.Add("ID");
            wrapper.Exclude.Add("Bar");
    
            Application.EnableVisualStyles();
            Application.Run(new Form {Controls = {
                new PropertyGrid { Dock = DockStyle.Fill,
                    SelectedObject = wrapper}}});
        }
    }
    
    [TypeConverter(typeof(RowWrapper.RowWrapperConverter))]
    class RowWrapper
    {
        private readonly List<string> exclude = new List<string>();
        public List<string> Exclude { get { return exclude; } }
        private readonly DataRowView rowView;
        public RowWrapper(DataRow row)
        {
            DataView view = new DataView(row.Table);
            foreach (DataRowView tmp in view)
            {
                if (tmp.Row == row)
                {
                    rowView = tmp;
                    break;
                }
            }
        }
        static DataRowView GetRowView(object component)
        {
            return ((RowWrapper)component).rowView;
        }
        class RowWrapperConverter : TypeConverter
        {
            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return true;
            }
            public override PropertyDescriptorCollection GetProperties(
                ITypeDescriptorContext context, object value, Attribute[] attributes)
            {
                RowWrapper rw = (RowWrapper)value;
                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(
                    GetRowView(value), attributes);
                List<PropertyDescriptor> result = new List<PropertyDescriptor>(props.Count);
                foreach (PropertyDescriptor prop in props)
                {
                    if (rw.Exclude.Contains(prop.Name)) continue;
                    result.Add(new RowWrapperDescriptor(prop));
                }
                return new PropertyDescriptorCollection(result.ToArray());
            }
        }
        class RowWrapperDescriptor : PropertyDescriptor
        {
            static Attribute[] GetAttribs(AttributeCollection value)
            {
                if (value == null) return null;
                Attribute[] result = new Attribute[value.Count];
                value.CopyTo(result, 0);
                return result;
            }
            readonly PropertyDescriptor innerProp;
            public RowWrapperDescriptor(PropertyDescriptor innerProperty)
                : base(
                    innerProperty.Name, GetAttribs(innerProperty.Attributes))
            {
                this.innerProp = innerProperty;
            }
    
    
            public override bool ShouldSerializeValue(object component)
            {
                return innerProp.ShouldSerializeValue(GetRowView(component));
            }
            public override void ResetValue(object component)
            {
                innerProp.ResetValue(GetRowView(component));
            }
            public override bool CanResetValue(object component)
            {
                return innerProp.CanResetValue(GetRowView(component));
            }
            public override void SetValue(object component, object value)
            {
                innerProp.SetValue(GetRowView(component), value);
            }
            public override object GetValue(object component)
            {
                return innerProp.GetValue(GetRowView(component));
            }
            public override Type PropertyType
            {
                get { return innerProp.PropertyType; }
            }
            public override Type ComponentType
            {
                get { return typeof(RowWrapper); }
            }
            public override bool IsReadOnly
            {
                get { return innerProp.IsReadOnly; }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
I have some C# code that looks something like this System.Data.DataRow row; var table
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have a System.Data.DataTable which is populated by reading a CSV file which sets
On the class level, I have created reference: System::Data::Odbc::OdbcConnection Connection; in some method I
I have a C# program that uses System.Data.OracleClient to access an oracle database. The
I have a WPF Application using Fluent NHibernate 1.0 RTM and System.Data.SQLite 1.0.65 that
I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient
I have following requirement, I have C#/.Net console application, which refers to 'System.Data.Sqlite.dll' 'System.Data.Sqlite.dll'
When defining parameter type that is e.g. in System.Data you have no intellisense and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.