Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
3.00/5 (5 votes)
See more:
Hi Friends,

In my project I put one DataGrid for viewing database data. Now I want the cell value which I selected in the DataGrid.

for that I code

 private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 textBox1.Text = dataGrid1.SelectedItem.ToString();// TextSelection;
               
}


I want the code name only. :-\ that is 'cs11'. but I got

{ name = tuutusdydfh, code = cs11 }
.,

so I try these code also.

C#
// textBox1.Text = dataGrid1.SelectedCells[1].ToString();
  //textBox1.Text = dataGrid1.CurrentCell.Item.ToString();
  // textBox1.Text = dataGrid1.ItemsSource.ToString();
  //textBox1.Text = dataGrid1.Items.ToString();


but I cant get. I want the code name only, help me friends.

My Data Grid (xaml):-

<datagrid name="dataGrid1" itemssource="{Binding }" borderbrush="Gray" borderthickness="5" margin="45,91,30,116">
                 Background="LightGray"
                 RowBackground="SeaShell"
                 RowHeight="30"
                 ColumnWidth="150"
                 AlternatingRowremoved="Lavender"
                 HorizontalScrollBarVisibility="Visible"
                 VerticalScrollBarVisibility="Visible"
                 MouseDoubleClick="dataGrid1_MouseDoubleClick"
                 SelectionChanged="dataGrid1_SelectionChanged" /></datagrid>


Code :-

if (paramType == "Hall")
           {
               //var dbList = from recs in MainWindow.mcsEntity.mcs_hall orderby recs.mcs_Hall_Name
                    //        select recs;
               //dataGrid1.DataContext = dbList;
               dataGrid1.DataContext = from re in MainWindow.mcsEntity.mcs_hall
                                       orderby re.mcs_Hall_Code
                                       select new
                                       {
                                           name = re.mcs_Hall_Name,
                                           code = re.mcs_Hall_Code
                                       };


           }
Posted
Updated 23-Mar-16 5:22am
v6
Comments
Vineeth P Joseph 28-Feb-11 1:49am    
Hi Jagadees,
Please provide some more details.How do you set the itemsSource in the datagrid?
Sagotharan Jagadeeswaran 28-Feb-11 3:47am    
Hi Vineeth,
i add some code. Its enough or not?.

Hi,

you can use a class

C#
public class MyClass
{
    public string Name {get;set;}
    public int Code { get; set; }
}


and create in the Linqstatement no var. You create new objects from your class:

C#
dataGrid1.DataContext = from re in MainWindow.mcsEntity.mcs_hall
                                       orderby re.mcs_Hall_Code
                                       select new MyClass
                                       {
                                           Name = re.mcs_Hall_Name,
                                           Code = re.mcs_Hall_Code
                                       };


in your SelectionChanged event you convert the selectedItem to your class:
C#
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
               textBox1.Text = (e.AddedItems[0] as MyClass).Name;
            }
        }


Hope that helps
 
Share this answer
 
Comments
Sagotharan Jagadeeswaran 4-Mar-11 2:06am    
thanks.
I think that this[^] thread on MSDN has the solution you are looking for.
 
Share this answer
 
Comments
Sagotharan Jagadeeswaran 4-Mar-11 2:06am    
thanks
C#
for (int j = 0; j < dataGrid1.Columns.Count; j++)
            {
                for (int i = 0; i < dataGrid1.Items.Count - 1; i++)
                {
                    string s=(dataGrid1.Items[i] as DataRowView).Row.ItemArray[j].ToString();
                }
}
 
Share this answer
 

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