WPF以编程方式填充DataGrid [英] WPF Populate DataGrid programmatically
问题描述
我有一个包含DataGrid的页面.我需要从2个单独的来源中进行填充.我从文本文件中读取的前三列数据(列标题和随后的数据行).
之后,我需要添加1至100列,其中仅包含复选框.
我将创建第1 3列作为DataGridTextColumn类型,其余的列将作为DataGridCheckBoxColumn类型创建.
我*认为*我正确地做到了这点,除了三个问题.
1.我无法查看第1 3列中的数据.
2.当我尝试单击其余列中的复选框时,它们不会选中/取消选中
3.如果我双击复选框,则会收到错误消息-此视图不允许使用EditItem.
下面是创建此网格的代码.创建此对话框的Winforms版本时,我捕获了CurrentCellDirtyStateChanged提交了Checked/Unchecked状态,而CellValueChanged捕获了结果更改.这是我的复选框问题的一部分-我还没有找到WPF的等效项.
关于如何解决我的问题有什么建议吗?
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>
推荐答案
您要添加(3 + data.SelectedAssays.Count)列数,但仅提供三个数据字段
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 );
我建议使用DataTable而不是直接添加数据,因为列的类型不同.
谢谢
I suggest to use DataTable instead of direct data adding because the type of the columns are different.
thanks
只需输入e.Handled = true;在这两个事件中CurrentCellDirtyStateChanged CellValueChanged
我认为这会对您有所帮助.
Just put e.Handled = true; in both the events CurrentCellDirtyStateChanged CellValueChanged
I think it will help you.
这篇关于WPF以编程方式填充DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!