Silverlight / WPF - How to add text to the GridView in wpf?

Asked By Ram Raghavendra on 08-Sep-09 02:48 AM

Hi,

I am developing an UI app, in WPF, for the Log window, I have xaml code

<ListView x:Name="lstViewLogWindow" ItemsSource="{Binding}" Height="152">

<ListView.View>

<GridView>

<GridViewColumn Header="Message_Name" Width="Auto" DisplayMemberBinding="{Binding Path= Message_Name}" />

<GridViewColumn Header="DateTime" Width="Auto" DisplayMemberBinding="{Binding Path= DateTime}" />

</GridView>

</ListView.View>

</ListView>

 

So, I have a grid view with 2 headers Message_Name and DateTime, and I want to assign the data to the  2 columns... I am not able to get the data displayed by using

lstViewLogWindow.Items.Add(msg);

Actually I want to add some msg into First col and date time into second column,

Please help me in doing this.
Thanks

Ramm

re - Web Star replied to Ram Raghavendra on 08-Sep-09 03:16 AM

use this way

protected void LinkButton2_Click(object sender, EventArgs e)

   {

      ((TextBox)gvIngredient.HeaderRow.FindControl("lblMsg")).Text = "message here";

      ((TextBox)gvIngredient.HeaderRow.FindControl("lblDate")).Text = "date here";

   }

it didnt worked - Ram Raghavendra replied to Web Star on 08-Sep-09 03:29 AM

HI A K...

I couldnt get gvIngredient.HeaderRow ..

I cant add element to the grid view this way.. \

actually I will receive a msg from some other function i have to assign that msg to the first column of the grid view.. so the second column will be date time.

Please help me

Thanks

Ramm

Where do you bind the ItemsSource? - Michael Detras replied to Ram Raghavendra on 08-Sep-09 03:34 AM

Hi, you could bind the ItemsSource property to an ObservableCollection<Data> object where the Data is something like this:

public class Data
{
    public string Message_Name { get; set; }

    public DateTime DateTime { get; set; }
}
You could then add items to the ObservableCollection object instead of adding directly to the Items property.
Vasanthakumar D replied to Ram Raghavendra on 08-Sep-09 03:36 AM

Hi,

you can add the datas to datatable or dataset , and set the datatable as the datacontext for listview as


DataTable dt = new DataTable();
            dt.Columns.Add("Message_Name", typeof(System.String));
            dt.Columns.Add("DateTime", typeof(System.DateTime));

            DataRow dr = dt.NewRow();
            dr[0] = "test";
            dr[1] = DateTime.Now;
            dt.Rows.Add(dr);

            DataRow dr1 = dt.NewRow();
            dr1[0] = "test 2";
            dr1[1] = DateTime.Now.AddDays(1);
            dt.Rows.Add(dr1);

            DataRow dr2 = dt.NewRow();
            dr2[0] = "test 3";
            dr2[1] = DateTime.Now.AddDays(2);
            dt.Rows.Add(dr2);

            lstViewLogWindow.DataContext = dt;
I am able to see only one column... - Ram Raghavendra replied to Vasanthakumar D on 08-Sep-09 03:50 AM

System.Data.DataTable dt = new System.Data.DataTable();

dt.Columns.Add("Message_Name", typeof(System.String));

dt.Columns.Add("LogDate", typeof(System.DateTime));

DataRow dr = dt.NewRow();

dr[0] = msg;

dr[1] = DateTime.Now;

dt.Rows.Add(dr);

lstViewLogWindow.DataContext = dt;


As I see only one column, DateTime.now is not visible in the second column...Also, it shows only one log?? this will be called manytimes...

Please help me..

Vasanthakumar D replied to Ram Raghavendra on 08-Sep-09 05:03 AM

Hi,

<GridViewColumn Header="DateTime" Width="Auto" DisplayMemberBinding="{Binding Path= DateTime}" />

in  above XAML, you are given binding column name as 'DateTime'.

but in your datatable you are named as 'LogDate'.

change the column name as 'DateTime'.

And , you have added only one row to datatable, so it will show only one row.

 


DateTime is also added.. but I see only one column data - Ram Raghavendra replied to Vasanthakumar D on 08-Sep-09 07:21 AM

HI Vasanth..

I have changed the Header name..but it doesnt take it..

Actually I should list all the msg's in the log window.. so I should nt clear it everytime right(by creating new data table...)

My req is.. if I select 1. downloading of data.. then on click of button.. the log window should have the msg.. "downloading started.." 08/09/09 ... 2. downloading stopped... then log window msg should append to the first msg(previously printed....) " downloading stopped 08/09/09

downloading started.. 08/09/09

downloading stopped 08/09/09


But as of now i see only downloading started msg.. not the other msgs...

Please help me