c# - 更改 DataGrid 的列宽

标签 c# wpf datagrid datatable

我想更改 DataGrid 的列宽,但一直收到错误。

这就是我填写 DataGrid 的方式:

tabel.ItemsSource = Item.getItemByCategory(category).DefaultView;

getItemByCategoryItem 类的静态方法,它返回 DataTable

这就是我尝试调整列大小的方法:

tabel.Columns[1].Width = 100;

这是我收到的错误:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

由于某种原因,tabel.Columns.Count == 0。尽管在显示之后立即显示了 4 列。

我可以在调整列大小方面获得一些帮助吗?

最佳答案

尝试这样的事情:

XAML 文件:

<Window x:Class="DataGridView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="table" Loaded="table_Loaded" />
    </Grid>
</Window>

代码隐藏文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Data;

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


            table.ItemsSource = getItemByCategory().DefaultView;
        }

        public static DataTable getItemByCategory(int category = 0)
        {
            //ServiceReference.ServiceSoapClient instance = Database.getClient();
            DataTable tabel = new DataTable();
            DataRow linie;
            tabel.Columns.Add(new DataColumn("id", typeof(int)));
            tabel.Columns.Add(new DataColumn("name", typeof(string)));

            //tabel.Columns.Add(new DataColumn("price", typeof(int)));
            //tabel.Columns.Add(new DataColumn("inCart", typeof(bool)));
            //ServiceReference.ArrayOfString[] values = instance.getItemsByCategory(category);

            List<Foo> values = new List<Foo>();
            for (int i = 0; i < 10; i++)
            {
                values.Add(new Foo { ID = i, Name = "name " + i });
            }

            foreach (var v in values)
            {
                linie = tabel.NewRow();
                linie["id"] = v.ID;
                linie["name"] = v.Name;
                //linie["price"] = Convert.ToInt32(v[3]);
                //linie["inCart"] = false;
                tabel.Rows.Add(linie);
            }
            return tabel;
        }

        private void table_Loaded(object sender, RoutedEventArgs e)
        {
            table.Columns[0].Width = 100;
            table.Columns[1].Width = 200;
        }
    }

    public class Foo
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }


}

关于c# - 更改 DataGrid 的列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11491531/

相关文章:

wpf - 如何获取带有换行而不是截断文本的单元格的 WPF Datagrid?

c# - 如何代码重用apsx?

wpf - 命名空间 "clr-namespace:Project.ViewModels"中不存在名称 ViewModel

c# - 如何读取应用程序日志并根据 ASP.NET 中的源对其进行过滤?

WPF - 创建自定义 ItemsControl

c# - 如何将单元格样式应用于 DataGrid 单元格

c# - 如何向DataGridView添加数据

c# - 绑定(bind) DataGrid 中的 ComboBox header

c# - 将 AspNetCore.TestHost 与 EntityFrameworkCore.InMemory 一起使用丢失数据

c# - 我想验证 WPF 13 位数字和 2 个破折号中的 CNIC 号码