如何将datagrid列值绑定到datagrid外的文本框?

时间:2018-04-04 16:48:00

标签: c# wpf xaml

那么我想要做的就是将数据网格链接中的值与数据网格之外的文本框一起使用,这样如果我改变了" rate"在数据网格内,价格将根据我在数据网格内所做的变化动态调整FT AMNT价格。我发布了数据网格和文本框的图片。如果需要更多信息,请告诉我。谢谢!

XAML CODE

 <DataGrid.Columns>
            <DataGridTextColumn x:Name="dgPcs" Header="PCS" Width="40" 
Binding="{Binding pieces}"/>
            <DataGridTextColumn x:Name="dgCode" Header="CODE" Width="50"  
Binding="{Binding code}"/>
            <DataGridTextColumn x:Name="dgDescr" Header="DESCRIPTION OF 
ARTICLES" Width="180" Binding="{Binding item_description}"/>
            <DataGridTextColumn x:Name="dgWt" Header="WT" Width="50" 
Binding="{Binding weight}"/>
            <DataGridTextColumn x:Name="dgRate" Header="RATE" Width="50" 
Binding="{Binding rate, StringFormat='C'}"/>
            <DataGridTextColumn x:Name="dgDiscount" Header="DISCOUNT" 
Width="70" Binding="{Binding discount,StringFormat='C'}"/>
            <DataGridTextColumn x:Name="dgAmt" Header="AMOUNT" Width="70" 
Binding="{Binding amount,StringFormat='C'}"/>
            <DataGridTemplateColumn Width="*" Header="ACTION">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

<GroupBox Margin="788,382,19,0" VerticalAlignment="Top" 
RenderTransformOrigin="0.268,0.174" Width="230" Header="TOTAL" 
Height="auto">
        <StackPanel Orientation="Vertical" Margin="5">
            <StackPanel Orientation="Horizontal">
                <Label Content="FRT AMNT: " Margin="9,0"/>
                <TextBox x:Name="txtFreightAmt" Width="100" Text="{Binding 
freightAmt,StringFormat='C'}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="ACCESSORIAL: "/>
                <TextBox x:Name="txtAccess" Width="100" Text="{Binding 
accessorial,StringFormat='C'}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="COD FEE: " Margin="14,0"/>
                <TextBox x:Name="txtCodFee" Width="100" Text="{Binding 
codFee,StringFormat='C'}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="FRT DISC: " Margin="13,0"/>
                <TextBox x:Name="txtFrtDisc" Width="100" Text="{Binding 
freightDisc,StringFormat='C'}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="NET FRT: " Margin="15,0"/>
                <TextBox x:Name="txtNetFrt" Width="100" Text="{Binding 
freightNet,StringFormat='C'}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="BILL AMNT: " Margin="8,0"/>
                <TextBox x:Name="txtBillAmnt" Width="100" Text="{Binding 
billAmount,StringFormat='C'}"/>
            </StackPanel>
        </StackPanel>
    </GroupBox>

绑定属性代码

public List<Bills> GetBillData(string billId, int? otherId)
    {
        OdbcConnection con = GetConnection();
        DataTable dt;
        List<Bills> Bill = new List<Bills>();
        try
        {
            //string spName = "dbo.edi_getCorrectedBill";
            OdbcDataAdapter da = new OdbcDataAdapter();
            OdbcCommand sqlCmd = new OdbcCommand("{call dbo.edi_getCorrectedBill(?,?)}", con);
            da.SelectCommand = sqlCmd;
            sqlCmd.CommandType = CommandType.StoredProcedure;

            sqlCmd.Parameters.AddWithValue("@billId", OdbcType.NVarChar).Value = billId.ToString().Trim();
            sqlCmd.Parameters.AddWithValue("@otherId", OdbcType.Int).Value = otherId;

            dt = new DataTable();
            sqlCmd.CommandTimeout = 1000;
            da.Fill(dt);

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Bills b = new Bills();
                    b.billNum = dr["bill_num"].ToString().Trim();
                    b.shipperID = Convert.ToInt32(dr["Shipper_ID"]);
                    b.shipperName = dr["Shipper_name"].ToString().Trim();
                    b.shipperAddr1 = dr["Shipper_addr1"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["shprAddr2"].ToString().Trim()))
                        b.shprAddr2 = dr["shprAddr2"].ToString().Trim();

                    b.shipperCity = dr["Shipper_city"].ToString().Trim();
                    b.shipperState = dr["Shipper_state"].ToString().Trim();
                    b.shipperZip = dr["Shipper_zip"].ToString().Trim();
                    b.sPhone = dr["S_phone"].ToString().Trim();

                    b.consignID = Convert.ToInt32(dr["Consign_ID"]);
                    b.consignName = dr["Consign_name"].ToString().Trim();
                    b.consignAddr1 = dr["Consign_addr1"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["cneeAddr2"].ToString().Trim()))
                        b.cneeAddr2 = dr["cneeAddr2"].ToString().Trim();

                    b.consignCity = dr["Consign_city"].ToString().Trim();
                    b.consignState = dr["Consign_state"].ToString().Trim();
                    b.consignZip = dr["Consign_zip"].ToString().Trim();
                    b.cPhone = dr["C_phone"].ToString().Trim();

                    b.otherID = Convert.ToInt32(dr["Other_ID"]);
                    b.billName = dr["Bill_name"].ToString().Trim();
                    b.billAddr1 = dr["Bill_addr1"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["billAddr2"].ToString().Trim()))
                        b.billAddr2 = dr["billAddr2"].ToString().Trim();

                    b.billCity = dr["Bill_city"].ToString().Trim();
                    b.billState = dr["Bill_state"].ToString().Trim();
                    b.billZip = dr["Bill_zip"].ToString().Trim();
                    b.shipDate = dr["ship_date"].ToString().Trim();
                    b.org = dr["Org"].ToString().Trim();
                    b.dest = dr["Dest"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["Shipper_ref"].ToString().Trim()))
                        b.shipperRef = dr["Shipper_ref"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["Consign_ref"].ToString().Trim()))
                        b.cneeRef = dr["Consign_ref"].ToString().Trim();

                    b.term = dr["Term"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["Shrink_Wrap"].ToString().Trim()))
                        b.shrinkWrap = Convert.ToInt32(dr["Shrink_Wrap"]);

                    if (!string.IsNullOrEmpty(dr["Re_Weight_Class"].ToString().Trim()))
                        b.reWt = dr["Re_Weight_Class"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["Agent_Pro"].ToString().Trim()))
                        b.BYD = dr["Agent_Pro"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["fuel_sc"].ToString().Trim()))
                        b.fuelSC = Convert.ToDecimal(dr["fuel_sc"]);

                    if (!string.IsNullOrEmpty(dr["fuel_sc_percent"].ToString().Trim()))
                        b.fuelScPercent = Convert.ToDecimal(dr["fuel_sc_percent"]);

                    b.accessorial = Convert.ToDecimal(dr["accessorial"]);
                    b.fccod = Convert.ToBoolean(dr["Fccod"]);
                    b.codFee = Convert.ToDecimal(dr["cod_fee"]);
                    b.freightDisc = Convert.ToDecimal(dr["freight_disc"]);
                    b.freightAmt = Convert.ToDecimal(dr["freight_amt"]);
                    b.billAmount = Convert.ToDecimal(dr["bill_amount"]);
                    b.freightNet = Convert.ToDecimal(dr["freight_net"]);

                    if (!string.IsNullOrEmpty(dr["Status"].ToString().Trim()))
                        b.status = dr["Status"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dr["description"].ToString().Trim()))
                        b.description = dr["description"].ToString().Trim();

                    Bill.Add(b);
                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            CloseConnection(con);
        }
        return Bill;
    }

    public List<BillItems> GetBillItemData(string billId)
    {
        OdbcConnection con = GetConnection();
        DataTable dt;
        List<BillItems> BillItem = new List<BillItems>();
        try
        {
            //string spName = "dbo.edi_getCorrectedBill";
            OdbcDataAdapter da = new OdbcDataAdapter();
            OdbcCommand sqlCmd = new OdbcCommand("{call dbo.edi_getCorrectedBillItems(?)}", con);
            da.SelectCommand = sqlCmd;
            sqlCmd.CommandType = CommandType.StoredProcedure;

            sqlCmd.Parameters.AddWithValue("@billId", OdbcType.NVarChar).Value = billId.ToString().Trim();

            dt = new DataTable();
            sqlCmd.CommandTimeout = 1000;
            da.Fill(dt);

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    BillItems b = new BillItems();

                    b.pieces = Convert.ToInt32(dr["pieces"]);
                    b.code = dr["Code"].ToString().Trim();
                    b.item_description = dr["Description"].ToString().Trim();
                    b.weight = Convert.ToInt32(dr["weight"]);
                    b.rate = Convert.ToDecimal(dr["rate"]);
                    b.discount = Convert.ToDecimal(dr["discount"]);
                    b.amount = Convert.ToDecimal(dr["amount"]);
                    //if (!string.IsNullOrEmpty(dr["shprAddr2"].ToString().Trim()))
                    //    b.shprAddr2 = dr["shprAddr2"].ToString().Trim();
                    BillItem.Add(b);
                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            CloseConnection(con);
        }
        return BillItem;
    }

数据网格和总文本框

enter image description here

0 个答案:

没有答案