以datagridview显示数据

本文关键字:数据 显示 datagridview | 更新日期: 2023-09-27 18:06:52

我必须做一个DataGridView,或一个DataGrid,或一个表,或其他东西来显示其中的数据,像这样:

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";

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

以datagridview显示数据

如果您有静态数据,则可以使用Grid并在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模式,让自己熟悉绑定。(网上有大量的教程)。但既然你不想用谷歌搜索……下面是一个例子:

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; }
    }
}

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

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

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