Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Limitations on a bound datagrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TonyJ - 24 Oct 2007 13:38 GMT
Hello!

I'm unsure when I can use a bound datagrid and when I can't.
What limitations has a bound datagrid?

1. For example if I want to manipulate the data in the datasource before
displaying the data in the datagrid can I then use a bound datagrid?
2. For example if I only want to display some of the columns of a datasource
table can I then use a bound datagrid?
3. For example if I some of the columns in a datagrid should be readonly can
I then use a bound datagrid?

This is only some of the example.

So as a summary exactly what limitations does a bound datagrid have?

//Tony
Morten Wennevik [C# MVP] - 24 Oct 2007 16:26 GMT
Hi Tony,

See answers inline

> Hello!
>
[quoted text clipped - 3 lines]
> 1. For example if I want to manipulate the data in the datasource before
> displaying the data in the datagrid can I then use a bound datagrid?

Maybe, although I am not sure how.  Then again, my knowledge of the DataGrid is limited

> 2. For example if I only want to display some of the columns of a datasource
> table can I then use a bound datagrid?

Yes

> 3. For example if I some of the columns in a datagrid should be readonly can
> I then use a bound datagrid?

Yes

> This is only some of the example.
>
> So as a summary exactly what limitations does a bound datagrid have?
>
> //Tony

If you can, I would recomment using the DataGridView instead of a DataGrid as it is much easier to configure and manipulate.

Using a DataGrid:

        protected override void OnLoad(System.EventArgs e)
        {
            DataGrid grid = new DataGrid();
            grid.Dock = DockStyle.Fill;
            this.Controls.Add(grid);

            DataTable table = new DataTable("Table");
            table.Columns.Add("Id", typeof(Int32));
            table.Columns.Add("Name", typeof(String));
            table.Columns.Add("Hidden", typeof(String));

            DataRow newRow = table.NewRow();
            newRow["Id"] = 0;
            newRow["Name"] = "Zero";
            newRow["Hidden"] = "Don't display";
            table.Rows.Add(newRow);

            newRow = table.NewRow();
            newRow["Id"] = 1;
            newRow["Name"] = "One";
            newRow["Hidden"] = "Don't display this either";
            table.Rows.Add(newRow);

            grid.DataSource = table;

    // Styling a grid is done using Table- and ColumnStyle objects

            DataGridTableStyle tableStyle = new DataGridTableStyle();
            tableStyle.MappingName = "Table";

            DataGridTextBoxColumn colStyle = new DataGridTextBoxColumn();
            colStyle.MappingName = "Id";
            colStyle.HeaderText = "Id";
            colStyle.ReadOnly = true;
            tableStyle.GridColumnStyles.Add(colStyle);

            colStyle = new DataGridTextBoxColumn();
            colStyle.MappingName = "Name";
            colStyle.HeaderText = "Name";
            tableStyle.GridColumnStyles.Add(colStyle);

            grid.TableStyles.Add(tableStyle);
        }

Using a DataGridView:

        protected override void OnLoad(System.EventArgs e)
        {
            DataGridView grid = new DataGridView();
            grid.Dock = DockStyle.Fill;
            this.Controls.Add(grid);

            DataTable table = new DataTable("Table");
            table.Columns.Add("Id", typeof(Int32));
            table.Columns.Add("Name", typeof(String));
            table.Columns.Add("Hidden", typeof(String));
            DataRow newRow = table.NewRow();
            newRow["Id"] = 0;
            newRow["Name"] = "Zero";
            newRow["Hidden"] = "Don't display";
            table.Rows.Add(newRow);
            newRow = table.NewRow();
            newRow["Id"] = 1;
            newRow["Name"] = "One";
            newRow["Hidden"] = "Don't display this either";
            table.Rows.Add(newRow);

            grid.DataSource = table;
            grid.CellFormatting += new DataGridViewCellFormattingEventHandler(grid_CellFormatting);

            grid.Columns["Id"].ReadOnly = true;
            grid.Columns["Hidden"].Visible = false;
        }

        // each cell will call this method before drawn
        void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value != null && e.Value != DBNull.Value && e.ColumnIndex == 0)
            {
                if ((int)e.Value > 0)
                {
                    e.CellStyle.BackColor = Color.Green;
                    e.Value = "+" + e.Value;
                }
                else
                {
                    e.CellStyle.BackColor = Color.Red;
                    e.Value = "-" + e.Value;
                }
            }
        }

Using a DataGridView, you can easily substitute the DataTable with a List<T>, something I could not get to work with the ColumnStyle objects on the DataGrid.  Then again, this is quite possibly due to my lack of knowledge.

Signature

Happy coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.