在WPF中与数据网格绑定的数据如何绑定bcz直接数据表不与项目源绑定请更正我在哪里我错了这里是代码........... [英] Data binding with Data Grid in WPF how it is bind bcz direct data table not binding with item source please correct me where is i am wrong here is code...........

查看:46
本文介绍了在WPF中与数据网格绑定的数据如何绑定bcz直接数据表不与项目源绑定请更正我在哪里我错了这里是代码...........的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if ((dt.Rows.Count > 0))
    {
        for (i = 0; i <= dt.Rows.Count - 1; i++)
        {
            string[] s = dt.Rows[i]["Working_Year"].ToString().Split(new char[] { '_' });
            string[] a = s[0].Split(new char[] { '-' });
            string[] b = s[2].Split(new char[] { '-' });
            Variables.StartDate1 = a[0] + "/" + a[1] + "/" + a[2];
            Variables.EndDate1 = b[0] + "/" + b[1] + "/" + b[2];

            string dat = a[0].ToString() + "-" + common_function.GetMonth(int.Parse(a[1].ToString())) + "-" + a[2].ToString();
            string dat2 = b[0].ToString() + "-" + common_function.GetMonth(int.Parse(b[1].ToString())) + "-" + b[2].ToString();
            DataRow newRow = data.NewRow();
            newRow["Company ID"] = dt.Rows[i]["Compid"].ToString();
            newRow["Company Name"] = dt.Rows[i]["CompanyName"].ToString();
            newRow["Working_Year"] = dat.ToString() + "  -  " + dat2.ToString();
            data.Rows.Add(newRow);
        }
    }
    dataGrid_selectCompany.ItemsSource = data;
    dataGrid_selectCompany.DataContext = data;
    dataGrid_selectCompany.Columns(2).Width = 150;
    dataGrid_selectCompany.Columns(3).Width = 150;
}







这是xamal代码.......... 。






Here is xamal code ...........

<UserControl x:Class="Manawat_WPF.Master_Form.SelectCompanyxaml"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="500" Loaded="UserControl_Loaded">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="248" HorizontalAlignment="Left" Margin="0,52,0,0" Name="dataGrid_selectCompany" VerticalAlignment="Top" Width="500" SelectionChanged="dataGrid_selectCompany_SelectionChanged">
            <DataGrid.Columns>
                <DataGridHyperlinkColumn Header="Select" />
            </DataGrid.Columns>
        </DataGrid>
        <Label Content="Select Company" Height="28" HorizontalAlignment="Center" Margin="162,12,206,0" Name="lbl_selectComapny" VerticalAlignment="Top" Width="132" />
    </Grid>
</UserControl>

推荐答案

尝试添加:



try adding:

<datagridtextcolumn header="Year" binding="{Binding Working_Year}" />





进入您的XAML并查看是否有任何内容显示。

我有一种感觉绑定是有效的,但是因为你没有配置任何列绑定到它你没有看到任何东西。



特别是你已经设置这个:AutoGenerateColumns =False



into your XAML and see if anything starts displaying.
I have a feeling the binding is working, however because you have not configured any columns to bind to it you are not seeing anything.

Especially as you have set this: AutoGenerateColumns="False"


这篇关于在WPF中与数据网格绑定的数据如何绑定bcz直接数据表不与项目源绑定请更正我在哪里我错了这里是代码...........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆