简体   繁体   English

C#如何从DataGrid(不是DataGridView)提取文本?

[英]C# How do I extract Text from a DataGrid (Not DataGridView)?

I'm using a DataGrid (Not a DataGridView) to display some info, and once I press a, say a save button I want all the data to be moved to a listbox column wise... 我正在使用DataGrid(而不是DataGridView)显示一些信息,一旦按a,说一个保存按钮,我希望将所有数据明智地移动到列表框列中。

Help's appreciated guys! 帮忙,感激的家伙! :D :D

EDIT: (here's the code so I want the data from the columns below to be read) I tried this : MessageBox.Show(SMLDataGrid.Items[0].ToString()); 编辑:(这是代码,所以我想从下面的列中读取数据)我试过了: MessageBox.Show(SMLDataGrid.Items[0].ToString()); There's no .Value option either... or anything similar that I've seen 没有.Value选项...或我见过的任何类似东西

<DataGrid x:Name="SMLDataGrid" CanUserSortColumns="True" CanUserAddRows="False" AutoGenerateColumns="False"
        materialDesign:DataGridAssist.CellPadding="13 8 8 8" materialDesign:DataGridAssist.ColumnHeaderPadding="8" VerticalAlignment="Top">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding BaseIn}"
                Header="Base" x:Name="GrammarBaseColumn"
                EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
            <DataGridTextColumn Binding="{Binding PastIn}"
                Header="Past Form" x:Name="GrammarPastColumn"
                EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
            <DataGridTextColumn Binding="{Binding PastPIn}"
                Header="Past Participle Form" x:Name="GrammarPastPColumn"
                EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
            <DataGridTextColumn Binding="{Binding IesIn}"
                Header="Plural Form" x:Name="GrammarIngColumn"
                EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
            <DataGridTextColumn Binding="{Binding IngIn}"
                Header="Verb Form" x:Name="GrammarIesColumn"
                EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
        </DataGrid.Columns>
    </DataGrid>

EDIT:I've tried assigning the ItemSource of the DataGrid upon the Save Button press to another list as well through this: 编辑:我已经尝试通过单击“保存”按钮将DataGrid的ItemSource分配给另一个列表:

ListBox a = new ListBox();
a.ItemsSource = SMLDataGrid.Items;
MessageBox.Show(a.Items[0].ToString());

But the messagebox shows the name of the file + name of the class that holds all the strings.. 但是消息框显示文件名+包含所有字符串的类的名称。

You have probably set the ItemsSource property of the DataGrid to an IEnumerable<YourClass> (if you haven't you should) where YourClass is the class where the BaseIn, PastIn, PastPIn, ... properties are defined. 您可能已将DataGrid的ItemsSource属性设置为IEnumerable<YourClass> (如果尚未设置),其中YourClass是定义BaseIn,PastIn,PastPIn等属性的类。

You can set the ItemsSource of the ListBox the same way: 您可以通过以下方式设置ListBox的ItemsSource:

listBox.ItemsSource = SMLDataGrid.ItemsSource;

var ic = SMLDataGrid.Items.OfType<YourClass>().ToList();
MessageBox.Show(ic[0].BaseIn);

The ListBox has no concept of columns though but depending on what you are trying to do you could use a ListView with a GridView instead: http://www.wpf-tutorial.com/listview-control/listview-with-gridview 尽管ListBox没有列的概念,但是根据您要执行的操作,您可以将ListView与GridView结合使用: http : //www.wpf-tutorial.com/listview-control/listview-with-gridview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# 如何将文本从 label 拖放到 datagridview 中的特定单元格中 - c# How do I drag and drop text from label into a specific cell in a datagridview C#/DataTable/DataGrid/SQL - 如何从 DataGridView 将参数插入到存储过程中 - C#/DataTable/DataGrid/SQL - How can I insert Params into a stored procedure from DataGridView 如何在C#中创建DataGrid? - How do I create a DataGrid in C#? 如何在 C# 中将文本提取/插入到 RTF 字符串中 - How do I extract/insert text into RTF string in C# 如何在c#中提取一串文本 - How do I extract a string of text in c# 如何编写 xpath 以使用 Selenium 和 C# 从文本节点中提取日期 - How do I write xpath to extract a date from a text node using Selenium and C# 如何从C#中的DataGridView单元中提取以分号分隔的值 - How can I extract semicolon separated values from DataGridView cell in C# 如何从C#的HTML页面提取此文本? - How can I extract this text from an HTML page in C#? 如何从C#System.Windows.Form.HtmlElement中提取*立即*文本(即不是子代中的文本) - How do I extract the *immediate* text from a C# System.Windows.Form.HtmlElement (i.e. NOT the text in children) 当对datagridview进行C#过滤时,如何将textbox.Text中的文本添加到datagridview.CurrentCell中? - How can I add text from textbox.Text to datagridview.CurrentCell when datagridview is filtered C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM