我想将文本框中的下一行文本添加到gridview中的新行。
这是我当前的代码:
<Label x:Name="label1" Content="URL :" HorizontalAlignment="Left" Margin="22,27,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="146" Margin="64,30,0,0" TextWrapping="Wrap" Text="Enter URLs Here" GotFocus="TextBox_GotFocus" VerticalAlignment="Top" Width="555" AcceptsReturn="True"/>
<Button x:Name="button1" Content="Check & Index" HorizontalAlignment="Left" Margin="652,19,0,0" VerticalAlignment="Top" Width="134" Height="40" Click="Button1_Click"/>
<Grid Margin="0,0,0,0.333">
<DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Height="459" VerticalAlignment="Top" Width="1700" Margin="22,185,0,-121.666" AutoGenerateColumns="True" SelectionChanged="dataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="URL" Width="150" Binding="{Binding URL}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
后面的代码:
private void Button1_Click(object sender, RoutedEventArgs e)
{
dataGrid1.Items.Add(new { URL = textBox1.Text});
}
当前代码只是将textBox1中的所有文本插入1个单列中。我要说的是,我在textBox1中有5行,每行应添加到新行中。
答案 0 :(得分:4)
您可以split文本框1的行。
List<string> Urllines = theText.Split(
new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries
).ToList();
然后
private void Button1_Click(object sender, RoutedEventArgs e)
{
foreach(string strUrl in Urllines)
dataGrid1.Items.Add(new { URL = strUrl});
}
我们使用RemoveEntryEmpties
作为第二个参数,以避免空结果。