views:

90

answers:

1

I'm trying to retrieve columns based on their corresponding key.

"Select * from diamond where p_Id="+p_Id

I followed MSDN sample and they were doing it like this way:

        CurrencyManager cm = ((CurrencyManager)this.BindingContext[myDatabaseDataSet, "Items.ItemDiamond"]);
        IBindingList list = (IBindingList)cm.List;

but list's related view data only returns data of p_Id=1. What's wrong?.

Full Code:

    private void Viewdiamonds(int p_Id)
    {
        this.Cursor = Cursors.WaitCursor;


        frmDiamond frmd = new frmDiamond();

        CurrencyManager cm = ((CurrencyManager)this.BindingContext[myDatabaseDataSet, "Items.ItemDiamond"]);
        IBindingList list = (IBindingList)cm.List;

        frmd.ShowDialog(p_Id, this, list);

        this.Cursor = Cursors.Default;

    }
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {


        if (e.ColumnIndex == 1 && e.RowIndex != -1 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
        {
            DataGridViewCell cell = this.dataGridView1[e.ColumnIndex - 1, e.RowIndex];
            int p_Id = Convert.ToInt32(cell.Value);
            this.Viewdiamonds(p_Id);

        }
    }

Edit:

I've two related columns, Items and Diamond. I've a datagridview control that display data from Items table. I added a column of type LinkView, so when the user clicks ViewDiamond, a new form pops up and display Diamonds related to that Item. I looked at MSDN DataGridView control sample, and they implemented that using the code above. I did some tweaks to the code to fit my application. What's the wrong with the code above? Because it seems its not able to display the data according to its relative p_Id. Or I think there is somthing wrong with the p_Id, no matter its value, Currency manager seems to retrieve only data corresponed to p_Id = 1.

I hope this clarified the issue.

A: 

Never mind, I did it with sql rdr:

        BindingSource bs = new BindingSource();

        string connstring = "Data Source=MyDatabase.sdf;Persist Security Info=False";
        string sqlqry = "SELECT * from diamond where p_Id=" + p_Id;
        SqlCeConnection conn = new SqlCeConnection(connstring);
        SqlCeCommand cmd = new SqlCeCommand(sqlqry, conn);
        conn.Open();

        SqlCeDataReader rdr = cmd.ExecuteReader();
        bs.DataSource = rdr;
        rdr.Close();
        conn.Close();

Whats the equivalent using linq anyway?.

DanSogaard
var result = from d in MyDbContext.diamond where d.p_Id == p_Id select d;
David Archer
Yes I thought the same too, but couldn't bind this to my datagridview. I tried to bind this to a list, then read the list from the dgv, but couldn't. Do you know how?.
DanSogaard
Alright, here it is:var qry = from d in myDatabaseDataSet.Diamond where d.p_Id == p_Id select d;bs.DataSource = qry.AsDataView();
DanSogaard