Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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></string>
Posted

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


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

MIDL
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
 
Share this answer
 
Just put e.Handled = true; in both the events CurrentCellDirtyStateChanged CellValueChanged

I think it will help you.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900