如何向WPF数据网格添加行 [英] how to add rows to WPF datagrids
问题描述
大家好,我在数据网格行为不端时遇到严重问题。
我的数据网格如下栏目
JobID,职位,上次运行,下次运行,时间表
i有如下变量
hi guys i am having serious problems with misbehaving of datagrids.
my datagrid as the following columns
JobID, Job title , last run, next run , schedule
i have variables as follows
int ID =int.Parse(reader[0].ToString());
string title = reader[2].ToString();
int jobint =int.Parse(reader[3].ToString());
DateTime lastrun = DateTime.Now;
DateTime nextrun = lastrun.AddSeconds(jobint);
和时间表是一个字符串值。
i需要在数据网格中添加一些记录,其中包含这些变量请有人帮助我
[edit]仅限受试者:'多个感叹号,'他继续说道摇头,这是一个患病心理的明确迹象。 Terry Pratchett [ ^ ] - OriginalGriff [/ edit]
and schedule is a string value .
i need to add a record to the datagrid with these variables in it please can someone assist me
[edit]Subject only: "'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind.'" Terry Pratchett[^] - OriginalGriff[/edit]
推荐答案
您好,
使用此处的教程时
I Programmer
我通过这个例子进行了调整,并根据你的需要进行了调整。
Hi there,
While using a tutorial found here
I Programmer
I worked through the example and adapted it for your needs.
public partial class MainWindow : Window
{
public struct MyData
{
public int id { set; get; }
public string title { set; get; }
public int jobint { set; get; }
public DateTime lastrun { set; get; }
public DateTime nextrun { set; get; }
}
public MainWindow()
{
InitializeComponent();
DataGridTextColumn col1 = new DataGridTextColumn();
DataGridTextColumn col2 = new DataGridTextColumn();
DataGridTextColumn col3 = new DataGridTextColumn();
DataGridTextColumn col4 = new DataGridTextColumn();
DataGridTextColumn col5 = new DataGridTextColumn();
myDataGrid.Columns.Add(col1);
myDataGrid.Columns.Add(col2);
myDataGrid.Columns.Add(col3);
myDataGrid.Columns.Add(col4);
myDataGrid.Columns.Add(col5);
col1.Binding = new Binding("id");
col2.Binding = new Binding("title");
col3.Binding = new Binding("jobint");
col4.Binding = new Binding("lastrun");
col5.Binding = new Binding("nextrun");
col1.Header = "ID";
col2.Header = "title";
col3.Header = "jobint";
col4.Header = "lastrun";
col5.Header = "nextrun";
myDataGrid.Items.Add(new MyData { id = 1, title = "Test", jobint = 2, lastrun = new DateTime(), nextrun = new DateTime() });
myDataGrid.Items.Add(new MyData { id = 12, title = "Test2", jobint = 24, lastrun = new DateTime(), nextrun = new DateTime() });
}
}
上面显示的代码似乎相当简单。 XAML如下:
The code behind shown above seems to be fairly straight forward. The XAML is as follows:
<Window x:Class="DataGridTester.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 x:Name="myDataGrid"/>
</Grid>
</Window>
希望这会有所帮助
劳伦斯
(我发布这个答案时遇到了很多问题,如果没有正确显示,我会道歉)
Hope this helps
Laurence
(I've had a lot of problems posting this answer, apologies if it doesn't display correctly)
首先你需要添加DataTable,比如
First you need to add in DataTable like
DataRow dr = dt.NewRow();
dr["ID"] =int.Parse(reader[0].ToString());
dr["title"] = reader[2].ToString();
dr["jobint"] =int.Parse(reader[3].ToString());
dr["lastrun"] = DateTime.Now;
dr["nextrun"] = lastrun.AddSeconds(jobint);
dt.Rows.Add(dr);
//BindGridViwe
Gridview1.DataSource = dt;
Gridview1.DataBind();
谢谢,
Imdadhusen
Thanks,
Imdadhusen
这篇关于如何向WPF数据网格添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!