在datagridview中显示数据

时间:2016-09-13 12:42:57

标签: c# wpf datagridview

我必须创建一个DataGridView,一个DataGrid,一个Table或其他东西来显示其中的数据,如下所示:

header: country   city         language
        usa       washington   eng
        germany   berlin       ger
        italy     rome         ita

我试过了:

dgv.Columns.Add(new DataGridViewColumn() 
    {Width=100, HeaderText ="ColumName1", CellTemplate 
        = new DataGridViewTextBoxCell() });
dgv.Columns.Add(new DataGridViewColumn() {Width=100, HeaderText = 
    "ColumName2", CellTemplate = new DataGridViewTextBoxCell() });
dgv.Columns.Add(new DataGridViewColumn() {Width=100, HeaderText = 
    "ColumName3", CellTemplate = new DataGridViewTextBoxCell() });



dgv.Rows.Add(new DataGridViewRow());
dgv.Rows.Add(new DataGridViewRow());
dgv.Rows.Add(new DataGridViewRow());

dgv.Rows[0].Cells[0].Value= "asdasd";
dgv.Rows[1].Cells[0].Value = "asdasd";
dgv.Rows[2].Cells[0].Value = "asdasd";

没有构建错误,当它启动时,它只显示左上角的网格尺寸,并且它是空的。

2 个答案:

答案 0 :(得分:0)

如果您有静态数据,则可以使用网格并在XAML中执行所有操作。

  <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
            <TextBlock Text="alöskdjf"
                       Grid.Row="0"
                       Grid.Column="1" />
            <TextBlock Text="alöskdjf"
                       Grid.Row="0"
                       Grid.Column="2" />
            <TextBlock Text="alöskdjf"
                       Grid.Row="1"
                       Grid.Column="0" />
            <TextBlock Text="alöskdjf"
                       Grid.Row="1"
                       Grid.Column="1" />
    </Grid>

如果您需要更高级的东西,您应该真正谷歌获取MVVM模式,并熟悉Bindings。 (网上有很多教程)。 但是因为你不想谷歌...这是一个例子:

MainWindow.xaml:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <!--Set the items source to the DataRows Property of your DataContext (MyViewModel1)-->
        <DataGrid ItemsSource="{Binding Path=DataRows}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Column A" />
                <DataGridTextColumn Header="Column B" />
                <DataGridTextColumn Header="Column C" />
                <DataGridTextColumn Header="Column D" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //create a new viewModel
            MyViewModel1 dataContext = new MyViewModel1();
            dataContext.DataRows = new List<MyDataRow1>();


            //create the datarows (you have to read your file here instead of adding 10 rows ;-)
            for (int i = 0; i < 10; i++)
            {
                dataContext.DataRows.Add( new MyDataRow1() { A = "öasldkfj" , B="aösldkfj", C="sdkljgfskn", D="asdfasdf"});
            }

            DataContext = dataContext;
        }
    }
    /// <summary>
    /// This is the ViewModel it provides Data data you can bind to from the UI
    /// </summary>
    public class MyViewModel1
    {
        /// <summary>
        /// Gets or sets the data rows.
        /// </summary>
        /// <value>The data rows.</value>
        public List<MyDataRow1> DataRows { get; set; }
    }

    public class MyDataRow1
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public string D { get; set; }
    }
}

答案 1 :(得分:0)

您正在创建Windows窗体应用程序。从那里,您可以在设计器中配置DataGridView的列。

这是如何从代码设置行单元格(根据您的简单示例)

 dgv.Rows.Add();
 dgv.Rows[0].Cells[0].Value = "sdfs";