Click here to Skip to main content
15,507,755 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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);


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]
Posted
Updated 21-Nov-18 20:09pm
v2
Comments
Anupama Roy 10-Feb-11 4:23am    
Is this Grid bound to a datasource already & do you want to add one more row to it ?
Unforgiv3n 10-Feb-11 4:30am    
there is no datasource bound to it there is no database.

i simply want to construct the rows from the variables and display it in the datagrid

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

The code behind shown above seems to be fairly straight forward. The XAML is as follows:

XML
<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)
 
Share this answer
 
v2
Comments
Unforgiv3n 10-Feb-11 8:23am    
thanks it worked perfectly
Laurence1234 10-Feb-11 8:48am    
Thanks, no problem :)
If you're interested the website I recommended has got a nice tutorial on achieving the some of the same results just in XAML, such as the bindings. Which I think is more elegant to split that out from the code behind.
Member 10659186 4-Mar-15 18:03pm    
Just what i needed. Thank you.
First you need to add in DataTable like
C#
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();


Thanks,
Imdadhusen
 
Share this answer
 
Comments
Unforgiv3n 10-Feb-11 4:26am    
the DATAGRID does not contain the following

datagrid.datasource ?

and the newrow function creates the row but when i do i row count on the table it return 0 ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900