0

I have 5 TextBoxes and a Button, what I want is to display 4 out of 5 textboxes values in the data grid when the button is clicked. I am a new user so i can not attach the picture of the interface.

My text boxes are named as follows:

IDTextbox, titleText, QuantityTextbox, PriceTextbox, BrandTextbox

My button is named AddButton.

When I click on AddButton, the values from titleText, QuantityTextbox, PriceTextbox, BrandTextbox should be displayed in dataGrid.

I don't want to display the product id in the datagrid, instead I want a column with the header "Total_price". This column should display the total_price ie (quantity * price)

Also, I want to add the values every time a click the AddButton.

Thanks

3 Answers 3

1

use this code

<Grid>
<Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
</Grid.ColumnDefinitions>
<DataGrid Name="DataGrid" ItemsSource="{Binding YourDataCollection}" />
<Grid Grid.Column="1">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="{Binding SelectedItem.Id, 
        ElementName=DataGrid}" />
    <TextBlock Grid.Row="1" Text="{Binding SelectedItem.Name, 
        ElementName=DataGrid}" />
    <TextBlock Grid.Row="2" Text="{Binding SelectedItem.BirthDate, 
        ElementName=DataGrid}" />
</Grid>

Sign up to request clarification or add additional context in comments.

Comments

1

Do Some thing Like that.....

<TextBox Name="IDTextbox" AcceptsReturn="True" TextWrapping="Wrap" SpellCheck.IsEnabled="True" Language="en-US" />
<TextBox Name="titleText" AcceptsReturn="True" TextWrapping="Wrap" SpellCheck.IsEnabled="True" Language="en-US" />
<TextBox Name="QuantityTextbox" AcceptsReturn="True" TextWrapping="Wrap" SpellCheck.IsEnabled="True" Language="en-US" />
<TextBox Name="PriceTextbox" AcceptsReturn="True" TextWrapping="Wrap" SpellCheck.IsEnabled="True" Language="en-US" />
<TextBox Name="BrandTextbox" AcceptsReturn="True" TextWrapping="Wrap" SpellCheck.IsEnabled="True" Language="en-US" />
<Button Content="Add new row" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

<DataGrid AutoGenerateColumns="False" Name="DataGrid1" CanUserAddRows="True" ItemsSource="{Binding TestBinding}" Margin="0,50,0,0" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Brand" IsReadOnly="True" Binding="{Binding Path=Brand}" Width="50"></DataGridTextColumn>
                <DataGridTextColumn Header="Title" IsReadOnly="True"  Binding="{Binding Path=Title}" Width="130"></DataGridTextColumn>
                <DataGridTextColumn Header="Quantity" IsReadOnly="True"  Binding="{Binding Path=Quantity}" Width="130"></DataGridTextColumn>
                <DataGridTextColumn Header="Price" IsReadOnly="True" Binding="{Binding Path=Price}" Width="130"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var data = new Test { Brand= BrandTextbox.Text, Title= titleText.Text,
                Quantity = QuantityTextbox.Text, Price = PriceTextbox.Text };

        DataGridTest.Items.Add(data);
    }
}

public class Test
{
    public string Brand { get; set; }
    public string Title { get; set; }
    public string Price { get; set; }
    public string Quantity { get; set; }
}

5 Comments

what if i want to add Up all the prices in the price column of dataGrid and show them below in a label? @jitendra
You can add one more row with grand total in data object... so do this--- data.add(new Test { Brand = "", Quality = "", Title = "Total" , Price = Data.Sub(l => l.Price)}); before line DataGridTest.Items.Add(data); @MohammadHaseeb . Currently i don't have visual Studio, so please let me know if u will find any issue...........
thanks for your help. This code has some errors there "Data.Sub(l => l.Price)"
U get it very right i just want to show the grand total at the bottom of the data grid
sorry use "Data.Sum(l => l.Price)" instead of "Data.Sub(l => l.Price)".... Thank You.... i have also found a useful thread for it ............ stackoverflow.com/questions/34484438/…
1

in the Xaml

<StackPanel>
    <TextBox x:Name="txtID" Height="50"></TextBox>
    <TextBox x:Name="txtQuantity" Height="50"></TextBox>
    <TextBox x:Name="txtPrice" Height="50"></TextBox>
    <TextBox x:Name="txtBrand" Height="50"></TextBox>
    <TextBox x:Name="txtTitle" Height="50"></TextBox>
    <Button Click="Button_Click_1" Height="20">Click</Button>
    <DataGrid x:Name="gridTotal">

    </DataGrid>
</StackPanel>

in the windows Loaded event

DataTable dt = new DataTable();
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

                if (!dt.Columns.Contains("Title"))
                {
                    dt.Columns.Add("Title");
                }
                if (!dt.Columns.Contains("Quantity"))
                {
                    dt.Columns.Add("Quantity");
                }
                if (!dt.Columns.Contains("Price"))
                {
                    dt.Columns.Add("Price");
                }
                if (!dt.Columns.Contains("Brand"))
                {
                    dt.Columns.Add("Brand");
                }
                if (!dt.Columns.Contains("Total"))
                {
                    dt.Columns.Add("Total");
                }

                gridTotal.ItemsSource = dt.DefaultView;

            }
            catch (Exception ec)
            {
                throw ec;
            }
        }

in Button click

 private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DataRow dr = dt.NewRow();
            dr["Title"] = txtTitle.Text;
            dr["Quantity"] = txtQuantity.Text;
            dr["Price"] = txtPrice.Text;
            dr["Brand"] = txtBrand.Text;
            dr["Total"] = Convert.ToDouble(txtQuantity.Text) * Convert.ToDouble(txtPrice.Text);

            dt.Rows.Add(dr);
        }

1 Comment

i want to add Up all the prices in the price column of dataGrid and show them at the bottom of the grid, can you please help me in this context?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.