从DataGrid C#WPF获取数据

时间:2019-02-14 05:43:51

标签: c# database wpf visual-studio

如何从datagrid获取数据?

combobox xml代码:

我将组合连接到表MahName

combobox1.ItemsSource = database.Mahs.ToList();

combobox1.DisplayMemberPath = "MahName";

这是代码datagrid

    <DataGrid.Columns>
        <DataGridTextColumn Header="کد محصول" Binding="{Binding MahID}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="نام محصول" Binding="{Binding MahName}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header=" قیمت فروش" Binding="{Binding MahFe}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده اول" Binding="{Binding Mah1}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده دوم" Binding="{Binding Mah2}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده سوم" Binding="{Binding Mah3}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده چهارم" Binding="{Binding Mah4}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>


        <DataGridTextColumn Header="نام ماده پنجم" Binding="{Binding Mah5}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده ششم" Binding="{Binding Mah6}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده هفتم" Binding="{Binding Mah7}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده هشتم" Binding="{Binding Mah8}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده نهم" Binding="{Binding Mah9}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="نام ماده دهم" Binding="{Binding Mah10}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده اول" Binding="{Binding Mahg1}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده دوم" Binding="{Binding Mahg2}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده سوم" Binding="{Binding Mahg3}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده چهارم" Binding="{Binding Mahg4}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>


        <DataGridTextColumn Header="وزن ماده پنجم" Binding="{Binding Mahg5}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده ششم" Binding="{Binding Mahg6}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده هفتم" Binding="{Binding Mahg7}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده هشتم" Binding="{Binding Mahg8}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده نهم" Binding="{Binding Mahg9}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header="وزن ماده دهم" Binding="{Binding Mahg10}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />



       <Setter Property="HorizontalContentAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>






<DataGridTextColumn Header=" قیمت ماده اول" Binding="{Binding Mahf1}">
                    <DataGridTextColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontFamily" Value="B Yekan" />
                            <Setter Property="FontSize" Value="12" />

                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                        </Style>
                    </DataGridTextColumn.HeaderStyle>
                </DataGridTextColumn>
        <DataGridTextColumn Header=" قیمت ماده دوم" Binding="{Binding Mahf2}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header=" قیمت ماده سوم" Binding="{Binding Mahf3}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header=" قیمت ماده چهارم" Binding="{Binding Mahf4}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header=" قیمت ماده پنجم" Binding="{Binding Mahf5}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header=" قیمت ماده ششم" Binding="{Binding Mahf6}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header=" قیمت ماده هفتم" Binding="{Binding Mahf7}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>


        <DataGridTextColumn Header=" قیمت ماده هشتم" Binding="{Binding Mahf8}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

        <DataGridTextColumn Header=" قیمت ماده نهم" Binding="{Binding Mahf9}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>





        <DataGridTextColumn Header="سود" Binding="{Binding Mahd}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />

                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>

    </DataGrid.Columns>

</DataGrid>

我想当combobox.text = datagrid中的“ MahName”然后datagrid / 1000中的“ Mahg1”然后* datagrid中的“ Mahf1” ...

数据网格/ 1000中的“ Mahg10”,然后*数据网格中的“ Mah10”

Mahf是价格,而Mahg是重量

我该怎么办?

0 个答案:

没有答案