Creating Generic UserControl
I want to create a generic user control that inherits from DataGridView and exposes a strongly typed property for binding to custom objects. My issue is that when I try to put in on any forms the designer pukes and won't paint the form, much less the control on the form. Is it even possible to have a generic user control that will work with the WinForm designer and if it is, what am I missing? Thanks.
Here is what the class file would look like.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
public class WinClientDataGridView<T> : DataGridView
{
private System.ComponentModel.IContainer components = null;
public WinClientDataGridView()
{
InitializeComponent();
//set the default properties for the grid
this.AllowUserToAddRows = false;
this.AllowUserToDeleteRows = false;
this.AllowUserToOrderColumns = true;
this.AllowUserToResizeColumns = true;
this.AllowUserToResizeRows = false;
this.AutoGenerateColumns = false;
this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.EditMode = DataGridViewEditMode.EditProgrammatically;
this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
this.ReadOnly = true;
this.MultiSelect = false;
this.AlternatingRowsDefaultCellStyle.BackColor = Color.Gainsboro;
this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.RowHeadersVisible = false;
this.CellBorderStyle = DataGridViewCellBorderStyle.None;
}
public <T> SelectedObject
{
get
{
if (this.SelectedRows.Count != 1) return null;
else return (T)this.SelectedRows[0].DataBoundItem;
}
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
}

