Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
In my WPF project (using C#) I have an XAML that contains a DataGrid. DataGrid has 4 columns headers (first one empty). On load I add a set of rows and add in the second cell of each row a string. I managed to add them, I guess, but they don't seem to be visible or maybe not added at all! I tried changing the color of text but couldn't find an easy way.

I'm new to WPF, so please can someone tell me what's the easiest way to add new rows and set their cells's text and their font color??

This is my xaml:
XML
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <DataGrid x:Name="DGV" HorizontalAlignment="Left" Margin="10,60,-88,0" VerticalAlignment="Top" Width="554" Height="103" Panel.ZIndex="1" Rowremoved="#FFC6C6C6" FontWeight="Bold" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" MinColumnWidth="10" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header=" "/>
            <DataGridTextColumn Header="URL" Width="295"/>
            <DataGridTextColumn Header="Ahrefs(http)" Width="79"/>
            <DataGridTextColumn Header="Ahrefs(www.)" Width="79"/>
            <DataGridTextColumn Header="Archive" Width="79"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>


And this is my xaml.cs:
C#
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 WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public struct MyDomain
        {
            public string id { set; get; }
            public string url { set; get; }
            public string AhrefsHttp { set; get; }
            public string AhrefsWww { set; get; }
            public string Archive { set; get; }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<string> lst;

            lst = new List<string>();

            lst.Add("google.com");
            lst.Add("yahoo.com");
            lst.Add("facebook.com");

            foreach (string link in lst)
            {
                DGV.Items.Add(new MyDomain { id = " ", url = link, AhrefsHttp = "", AhrefsWww = "", Archive = "" });
            }
        }
    }
}
Posted
Updated 4-Aug-14 12:38pm
v2
Comments
Sergey Alexandrovich Kryukov 4-Aug-14 18:09pm    
What is the problem?
—SA
saleem_deek 4-Aug-14 18:39pm    
Sergey, I added my full code above, when executed, 3 rows are added to my DG but text is not visible, as if not added!
And how to set the text in other cells on another event (not at the same moment when I add the row)

It makes absolutely no sense to put any style inside of the XAML element. If you could, how could you possibly reuse it? If you don't need a style reuse, why having it at all?

If you need to do it for only one element, you can directly add all those properties to it:
XML
<DataGrid FontSize="...", FondFamily="..." FontWeigh="..." ... >
   <!-- ... -->
</DataGrid>


If you need to be consistent with other elements, possibly of different type, for consistency, you can create a resource dictionary and reference all the property from this dictionary one by one. Using Resource Dictionaries is explained here: http://msdn.microsoft.com/en-us/library/cc903952%28v=vs.95%29.aspx[^].

You can use the defined resource element using the StaticResource binding: http://msdn.microsoft.com/en-us/library/cc903952%28v=vs.95%29.aspx#referencing_resources_from_xaml[^].

You can define a resource dictionary separate from your window-type XAML using the merged resource dictionaries; this way, you can reuse the original external resource dictionary (referenced by its relative file name) in more than one window or user control type: http://msdn.microsoft.com/en-us/library/aa350178%28v=vs.110%29.aspx[^].

Finally, you can use styles are reuse them for different elements and different windows or user controls:
http://msdn.microsoft.com/en-us/library/ms745683%28v=vs.110%29.aspx[^],
http://wpftutorial.net/Styles.html[^].

—SA
 
Share this answer
 
1) I had to add binding resources:
<datagridtextcolumn header="URL" width="295" binding="{Binding url}">
<datagridtextcolumn header="Ahrefs(http)" width="79" binding="{Binding AhrefsHttp}">
<datagridtextcolumn header="Ahrefs(www.)" width="79" binding="{Binding AhrefsWww}">
<datagridtextcolumn header="Archive" width="79" binding="{Binding Archive}">

2) for cell style the links mentioned by other accepted solutions are helpful.

3) for editing a text in a cell programmatically: Click here
 
Share this answer
 
v4

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