Click here to Skip to main content
15,847,414 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have a three problems

1.i was select one cell but it selected all row

2.enter something in cell and to change cell the previous cell it will be empty

3.how to use combobox in grid view

XAML

XML
<DataGrid AutoGenerateColumns="False" Background="LightGray" RowBackground="Orange" AlternatingRowremoved="LightBlue" 
              Height="290" HorizontalAlignment="Left" Margin="29,178,0,0" Name="dgrid_familyDetails" VerticalAlignment="Top" 
              Width="854" BeginningEdit="dgrid_familyDetails_BeginningEdit" FontSize="14">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" x:Name="Name1" Width="100"/>
            <DataGridTextColumn Header="Age" x:Name="Age1" Width="50"/>
            <DataGridComboBoxColumn Header="Marital Status" x:Name="Married" Width="85"/>
            <DataGridComboBoxColumn Header="Gender" x:Name="Gender"/>
            <DataGridTextColumn Header="Qualification" />
        </DataGrid.Columns>
    </DataGrid>


C#

C#
public List<string> _maritalStatus { get; set; }
    public List<string> _Gender { get; set; }
    public MainWindow()
    {
        InitializeComponent();

        DataTable dt = new DataTable();
        dgrid_familyDetails.ItemsSource = dt.DefaultView;
        dt.Rows.Add();
        _maritalStatus = new List<string>() { "Single", "Married" };
        _Gender = new List<string>() { "Male", "Female" };
        Married.ItemsSource = _maritalStatus;
        Gender.ItemsSource = _Gender;
    }


Please give a solution....

thanks
Posted

1 solution

ComboBox in DataGrid
I came across a question that how to display combo box in Data Grid. It is not different than displaying it in ListView we saw here. Similarly we have need to make a nested data structure first. Here is our data class.


code:
CSS
public class State
{
    public string Name
    { get; set; }

    public string TimeZone
    { get; set; }

    public string Capital
    { get; set; }

    public ObservableCollection<string> Cities
    { get; set; }
}


Then we populate it with some data.
Code:
XML
states.Add(new State()
{
    Name = "Maryland",
    Capital = "Annapolis",
    TimeZone = "Eastern",
    Cities = new ObservableCollection<string>() { "Frederick", "Baltimore", "Rockville"}
});

states.Add(new State()
{
    Name = "Taxes",
    Capital = "Austin",
    TimeZone = "Central",
    Cities = new ObservableCollection<string>() { "Houston", "Dallas", "San Antonio" }
});

states.Add(new State()
{
    Name = "Utah",
    Capital = "Salt Lake City",
    TimeZone = "Mountain",
    Cities = new ObservableCollection<string>() { "West Valley City", "Provo", "West Jordon" }
});

states.Add(new State()
{
    Name = "California",
    Capital = "Sacramento",
    TimeZone = "Pacific",
    Cities = new ObservableCollection<string>() { "Los Angeles", "San Fransisco", "San Diego" }
});


Then we define DataGridTemplateColumn and define data template for our column where we want to display the combo box. Here is a piece of XAML code for this.
Code:
XML
<DataGridTemplateColumn Header="Cities" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Margin="2" ItemsSource="{Binding Cities}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


Rest of the code is very simple. Here is our complete C# code of the program.
Code:
XML
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfDataGridCombo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<State> states = new ObservableCollection<State>();

        public MainWindow()
        {
            InitializeComponent();

            states.Add(new State()
            {
                Name = "Maryland",
                Capital = "Annapolis",
                TimeZone = "Eastern",
                Cities = new ObservableCollection<string>() { "Frederick", "Baltimore", "Rockville"}
            });

            states.Add(new State()
            {
                Name = "Taxes",
                Capital = "Austin",
                TimeZone = "Central",
                Cities = new ObservableCollection<string>() { "Houston", "Dallas", "San Antonio" }
            });

            states.Add(new State()
            {
                Name = "Utah",
                Capital = "Salt Lake City",
                TimeZone = "Mountain",
                Cities = new ObservableCollection<string>() { "West Valley City", "Provo", "West Jordon" }
            });

            states.Add(new State()
            {
                Name = "California",
                Capital = "Sacramento",
                TimeZone = "Pacific",
                Cities = new ObservableCollection<string>() { "Los Angeles", "San Fransisco", "San Diego" }
            });

            DataContext = states;
        }
    }

    public class State
    {
        public string Name
        { get; set; }

        public string TimeZone
        { get; set; }

        public string Capital
        { get; set; }

        public ObservableCollection<string> Cities
        { get; set; }
    }
}




Here is complete XAML code of our program.
Code:
<window x:class="WpfDataGridCombo.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Combo Box in Daga Grid" Height="300" Width="400">
<grid>
<datagrid margin="5" itemssource="{Binding}" autogeneratecolumns="False">
<datagrid.columns>
<datagridtextcolumn header="Name" binding="{Binding Name}">
<datagridtextcolumn header="Capital" binding="{Binding Capital}">
<datagridtextcolumn header="Time Zone" binding="{Binding TimeZone}">
<datagridtemplatecolumn header="Cities" width="*">
<datagridtemplatecolumn.celltemplate>
<datatemplate>
<combobox margin="2" itemssource="{Binding Cities}">







Code
 
Share this answer
 
v2
Comments
An@nd Rajan10 21-Nov-13 4:13am    
this not working that gives empty gridview and no row for edit

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