Reputation: 89
I would like to add the next line text in a textbox to a new line in gridview.
This is my current code:
<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>
Code Behind:
private void Button1_Click(object sender, RoutedEventArgs e)
{
dataGrid1.Items.Add(new { URL = textBox1.Text});
}
Current code just inserts all text from textBox1 into 1 single column. What I want is let's say I have 5 lines in textBox1, each line should be added into new row.
Upvotes: 0
Views: 154
Reputation: 14044
You can split the lines of the textbox1.
List<string> Urllines = theText.Split(
new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries
).ToList();
and then
private void Button1_Click(object sender, RoutedEventArgs e)
{
foreach(string strUrl in Urllines)
dataGrid1.Items.Add(new { URL = strUrl});
}
We use RemoveEntryEmpties
as the second parameter to avoid empty results.
Upvotes: 4