Click here to Skip to main content
12,078,742 members (47,242 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
I have a Page containing a DataGrid. I need to populate this from 2 separate sources. The 1st three columns of data I read from a text file (both the column headers, and the subsequent rows of data).
After that there is anywhere from 1 to 100 columns I need to add that will just contain checkboxes.
I'm creating the 1st 3 columns as type DataGridTextColumn, and the remaining columns I am creating as type DataGridCheckBoxColumn.
I *think* I'm doing this all correctly, except for three problems.

1. I can't SEE the data in the 1st 3 columns.

2. When I try to click on the Checkboxes in the remaining columns they don't Check / Uncheck

3. If I double-click the checkboxes I get an error - "EditItem is not allowed for this view.

Below is the code that creates this grid. When I created a Winforms version of this dialog, I trapped the CurrentCellDirtyStateChanged to commit the Checked/Unchecked state, and CellValueChanged to operate on the resulting change. That's part of my Checkboxes problem - I haven't found a WPF equivalent.
Any suggestions on how to solve my problems?
private void CreateMapSamplesToAssaysTable( )
{
    try
    {
        this.dgViewSamples.Items.Clear();
        this.dgViewSamples.Columns.Clear( );
        List<string> strColHeaders = new List<string>( );
        strColHeaders.Add( rm.GetString( "SampleIDColumnTitle" ) );
        strColHeaders.Add( rm.GetString( "SampleDescriptionColumnTitle" ) );
        strColHeaders.Add( rm.GetString( "DilutionFactorColumnTitle" ) );
        // Add columns and column header captions for the selected assays.
        int numAssays = data.SelectedAssays.Count;
        for( int nIdx = 0; nIdx < numAssays; nIdx++ )
        {
            string selectedAssayName = data.SelectedAssays[ nIdx ];
            strColHeaders.Add( selectedAssayName );
        }
        int nCols = strColHeaders.Count( );
        for( int nIdx = 0; nIdx < nCols; nIdx++ )
        {
            DataGridColumn col = null;
            switch( nIdx )
            {
                case 0:
                case 1:
                case 2:
                    col = new DataGridTextColumn( );
                    col.IsReadOnly = true;
                    break;
                default:
                    col = new DataGridCheckBoxColumn( );
                    col.IsReadOnly = false;
                    break;
            }
            col.Header = strColHeaders[ nIdx ];
            this.dgViewSamples.Columns.Add( col );
        }
        string strFileName = data.WorklistPath;
        StreamReader reader = new StreamReader( strFileName );
        string strLine = null;
        // Eat the first line, i.e. the column header
        strLine = reader.ReadLine( );
        while( ( strLine = reader.ReadLine( ) ) != null )
        {
            char[ ] chDelimiter = new char[ ] { '\t' };
            string[ ] strParts = strLine.Split( chDelimiter,
                StringSplitOptions.None );
            if( strParts.Length >= 3 )
            {
                string[] strRow = {strParts[0], strParts[1], strParts[2]};
                this.dgViewSamples.Items.Add( strRow );
            }
        }
        reader.Close( );
    }
    catch( Exception ex )
    {
        StackFrame sf = new StackFrame( );
        Errors.ErrorMessage( ex, sf, true );
    }
}</string>
Posted 21-Jun-11 15:24pm
MGScott793
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

you are adding (3 + data.SelectedAssays.Count) number of columns but supplying only three data fields
{strParts[0], strParts[1], strParts[2]};

so you have to specify for what all columns the data goes to

col.DisplayIndex = nIdx;
col.Header = strColHeaders[ nIdx ];
this.dgViewSamples.Columns.Add( col );

I suggest to use DataTable instead of direct data adding because the type of the columns are different.

thanks
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Just put e.Handled = true; in both the events CurrentCellDirtyStateChanged CellValueChanged

I think it will help you.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160212.1 | Last Updated 22 Jun 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100