Click here to Skip to main content
14,661,016 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Team,

I have a Datagrid with binding the itemssource as Datatable. The table results dynamically binding in the grid. So I haven't specify any columns in Xaml.

I have provided add, edit, delete option in the grid. The problem is I cannot add the multiline text in datagrid text box column. It accepts only the first row of the content.

What I have tried:

Here is my Xaml Code :

<pre><DataGrid
                        x:Name="autoBook_Datagrid"
                        HorizontalScrollBarVisibility="Auto"
                        VerticalScrollBarVisibility="Auto"
                        ScrollViewer.CanContentScroll="True"
                        BorderBrush="{StaticResource GreenBrush}"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        RowHeight="25"
                        GridLinesVisibility="All"
                        ClipboardCopyMode="IncludeHeader"
                        ItemsSource="{Binding DynamicBindingTable, UpdateSourceTrigger=PropertyChanged}"
                        CanUserAddRows="{Binding CanAddNewRows, UpdateSourceTrigger=PropertyChanged}"
                        VirtualizingPanel.VirtualizationMode="Recycling"
                        VirtualizingPanel.ScrollUnit="Pixel"
                        EnableColumnVirtualization="True"
                        EnableRowVirtualization="True"
                        FocusVisualStyle="{StaticResource ButtonFocusVisual}"
                            CanUserDeleteRows="False"
                        >



Normally we can set acceptsreturs property in textbox to update the multiline text. But here I don't know what to do in this case.

Could you please help me on this issue to update the multiline text in the datagrid text.

Regards,
Arunkumar Murugesan
Posted
Updated 14-Apr-20 19:35pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Check this: wpfdatagrid - WPF DataGridTextColumn multi-line input - Stack Overflow[^]

<DataGridTextColumn Header="Message" Binding="{Binding Path=Message}"  Width="Auto">
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
                <Setter Property="TextWrapping" Value="Wrap" />
            </Style>
        </DataGridTextColumn.ElementStyle>
        <DataGridTextColumn.EditingElementStyle>
            <Style TargetType="TextBox">
                <Setter Property="TextWrapping" Value="Wrap" />
                <Setter Property="AcceptsReturn" Value="true" />
            </Style>
        </DataGridTextColumn.EditingElementStyle>
    </DataGridTextColumn>
   
Comments
Arunkumar Murugesan 15-Apr-20 2:24am
   
Hi Maciej,

Thanks for your update. In your solution we know the column name and we can easily set the style. But in my case, I will load multiple table values into the grid. So I haven't mention any column name in xaml. I have binding the Datatable values directly into the Grid. Its a dynamic one.

For this case, How could I resolve this?
Maciej Los 15-Apr-20 2:36am
   
Arunkumar Murugesan 15-Apr-20 3:06am
   
Yes Maciej. I agree with it. But Here I have nearly 200 tables and I can get the result of each table from SQL and directly binding into the grid without preparing any columns. In this case we cannot use the multiple view right?

So in this case I just need to accepts the multiline text whenever the datagrid column getting edited.
Maciej Los 15-Apr-20 3:52am
   
I don't get you... Do you want to edit table data directly in WPF form? In WPF you are using MVVM pattern (Model-View -ViewModel) to "serve" data to the client. So, if your application does NOT implement MVVM patten, it makes no sense... Sorry.
Arunkumar Murugesan 15-Apr-20 4:22am
   
Actually what I have done so far is,

1. I have listed the tables in combobox. We will get the result from the SQL based the tables chosen. Each table having minimum 20 columns.
2. In this case, I have directly binding the Datatable into the Datagrid. All these I have implemented with MVVM pattern only
3. Also I have provided option in editing the cell values. We can double click the cell to paste the values to update.
4. For this case, I tried to paste the content in the grid cell like the mail body content. From this content, first row of the value only pasted in the grid cell. All the content will be pasted in grid cell. This is my issue actually.
Arunkumar Murugesan 15-Apr-20 4:23am
   
Can u understand what I trying to convey here?
Maciej Los 15-Apr-20 4:57am
   
Yes, i understand what you want to achieve, but this seems to be (very) strange to me.
Can't you just show TextBox under (or over) a datagrid to enable "mulitline" editing? This would be much easier.
[Edit]
BTW, take a look at this: Data Binding - A Better Way to Implement Data Binding in .NET | Microsoft Docs[^]

I'd implement something similar to this: Loading XAML Dynamically – Part 1 – Loading Views Dynamically – Bruno Sonnino[^]
Arunkumar Murugesan 15-Apr-20 5:03am
   
<datagrid
x:name="autoBook_Datagrid"
="" horizontalscrollbarvisibility="Auto" verticalscrollbarvisibility="Auto" scrollviewer.cancontentscroll="True" borderbrush="{StaticResource GreenBrush}" horizontalalignment="Left" verticalalignment="Top" rowheight="25" gridlinesvisibility="All" clipboardcopymode="IncludeHeader" itemssource="{Binding DynamicBindingTable, UpdateSourceTrigger=PropertyChanged}" canuseraddrows="{Binding CanAddNewRows, UpdateSourceTrigger=PropertyChanged}" virtualizingpanel.virtualizationmode="Recycling" virtualizingpanel.scrollunit="Pixel" enablecolumnvirtualization="True" enablerowvirtualization="True" focusvisualstyle="{StaticResource ButtonFocusVisual}" canuserdeleterows="False">

in this code, how could I add the textbox under datagrid? I'm getting confused. Can u see some code like what u said pls.
Maciej Los 15-Apr-20 5:05am
   
See updated comment.
Arunkumar Murugesan 15-Apr-20 5:24am
   
<datagrid autogeneratecolumns="False" x:name="master" canuseraddrows="False" canuserdeleterows="True" grid.row="1"
="" itemssource="{Binding Customers}" selecteditem="{Binding SelectedCustomer, Mode=TwoWay}">
<datagrid.columns>
<datagridtextcolumn x:name="customerIDColumn" binding="{Binding Path=CustomerId}" header="Customer ID" width="80">
<datagridtextcolumn x:name="companyNameColumn" binding="{Binding Path=CompanyName,ValidatesOnDataErrors=True}" header="Company Name" width="300">
<datagridtextcolumn x:name="cityColumn" binding="{Binding Path=City}" header="City" width="100">
<datagridtextcolumn x:name="countryColumn" binding="{Binding Path=Country}" header="Country" width="100">



In your code, you have defined the column what needs to be displayed in the grid. In this case we can easily add "AcceptReturs = True" for accepting the multiline text in the cell like below,


DataGridTemplateColumn.CellEditingTemplate>
<datatemplate>
<textbox text="{Binding Path=CompanyName, Mode=TwoWay}" acceptreturs="True" textwrapping="Wrap">



But in my case I cannot define the column names since its a dynamic tables. I think its a easier one. But my mind stuck somewhere.
Maciej Los 15-Apr-20 7:15am
   
This seems to be what tou want: WPF datagrid: How to set specifc setter in C# in stead of XAML - Stack Overflow[^]
You can add custom setter to text column programmatically.
Arunkumar Murugesan 15-Apr-20 7:54am
   
Will check this maciej. Thanks.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100