LINQ to dataset binding Datagrid - EditItem' is not allowed for this view

Nov 24, 2011 at 8:06 AM
Edited Nov 24, 2011 at 9:13 AM

Hi,

I'm Using Vs2010.  Newbie to Linq and binding datagrid.

I bind data using Linq to dataset. to Datagrid.

It's Binding data but when i edit or double click the grid I got Error "'EditItem' is not allowed for this view."

Here is my code Behind:

 

public void ConnectOracle_DB()
        {

            connectionString = "Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = AMA11)(PORT = 1521)))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = AMA11ORA))); User Id = tom; Password = pwd4tom;";

            cn = new OracleConnection(connectionString);

            cmd1 = new OracleCommand(@"select to_char(ID) as ID, to_char(MN) as MN, to_char(MG) as MG, to_char(TANKCD) as TANKCD from MEDIA", cn);
            cmd2 = new OracleCommand(@"select to_char(ID) as ID, NAME from TANK", cn);

            adapter1 = new OracleDataAdapter(cmd1);
            adapter2 = new OracleDataAdapter(cmd2);

            cmb1 = new OracleCommandBuilder(adapter1);
            cmb2 = new OracleCommandBuilder(adapter2);
        }


public void LoadData()
        {

          dataGrid1.SelectionMode = DataGridSelectionMode.Extended;

          ds = new DataSet();

          adapter1.Fill(ds, "MEDIA");
          adapter2.Fill(ds, "TANK");

          DataTable medias = ds.Tables["MEDIA"];
          DataTable tanks = ds.Tables["TANK"];


          var query = from med in medias.AsEnumerable()
                      join tnk in tanks.AsEnumerable()
                      on med.Field<string>("TANKCD") equals tnk.Field<string>("ID")
                      select new
                      {
                          TANKCD = med.Field<string>("TANKCD"),
                          MG = med.Field<string>("MG"),
                          MN = med.Field<string>("MN"),
                          NAME = tnk.Field<string>("NAME")
                      };


          foreach (var media_tank in query)
          {
              Console.WriteLine("tankcd: {0} "
                              + "MG: {1} "
                              + "MN: {2} "
                              + "NAME: {3}",
                  media_tank.TANKCD,
                  media_tank.MG,
                  media_tank.MN,
                  media_tank.NAME);
          }


          dataGrid1.DataContext = query;
          dataGrid1.ItemsSource = query; //ds.Tables[0];

        }

 

XAML:

<Grid>
        <DataGrid AutoGenerateColumns="False"
                  Height="200"
                  HorizontalAlignment="Left"
                  Margin="12,24,0,0"
                  Name="dataGrid1"
                  VerticalAlignment="Top"
                  Width="375"
                  ItemsSource="{Binding}">
            
            
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="TANKCD1"
                                    Binding="{Binding Path=TANKCD}"
                                    Header="TANK CD"
                                    IsReadOnly="False"  />
                <DataGridTextColumn x:Name="MG1"
                                    Binding="{Binding Path=MG}"
                                    Header="MG"
                                    IsReadOnly="False" />
                <DataGridTextColumn x:Name="MN1"
                                    Binding="{Binding Path=MN}"
                                    Header="MN" IsReadOnly="False" />
                <DataGridTextColumn x:Name="NAME"
                                    Binding="{Binding Path=NAME}"
                                    Header="NAME"
                                    IsReadOnly="False" />
            </DataGrid.Columns>
        </DataGrid>
        
    </Grid>


Thanks,

thenndral