Click here to Skip to main content
15,915,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,

I have two datagrids,in one form and i fill them with sql table data,from the same table,but different filter.Then i get some values from each selected row of datagrid,and fill some textboxes. I use the cell content click event to catch which datagrid is selected.The problem shows up when i select a row in one grid and a colum in second grid.The clear selection doesnt do the job. Any help??

Here is the code that i use to get the values depending on rowindex

C#
private void dataGridEksp_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            Global.MakeReadOnly(this.tabPage5);            
            i = dataGridEksp.SelectedRows[0].Index;
            txt_rrugaEks.Text = dataGridEksp.Rows[i].Cells[0].Value.ToString();
            cmb_grupi.Text = dataGridEksp.Rows[i].Cells[1].Value.ToString();
            txt_pagShofer.Text = dataGridEksp.Rows[i].Cells[2].Value.ToString();
            txt_pagPun.Text = dataGridEksp.Rows[i].Cells[3].Value.ToString();
            txt_pagPunetorEkstra.Text = dataGridEksp.Rows[i].Cells[4].Value.ToString();
            txt_pagShoferEkstra.Text = dataGridEksp.Rows[i].Cells[5].Value.ToString();
            txt_ujeEks.Text = dataGridEksp.Rows[i].Cells[6].Value.ToString();
            txt_emulEks.Text = dataGridEksp.Rows[i].Cells[7].Value.ToString();
            txt_nafteEks.Text = dataGridEksp.Rows[i].Cells[8].Value.ToString();
            txt_rereEks.Text = dataGridEksp.Rows[i].Cells[9].Value.ToString();

            idCmime = dataGridEksp.Rows[i].Cells[10].Value.ToString(); ;
            
            button_modifikoCmim.Enabled = true;
            dataGridBrendshem.ClearSelection();
            button_fshiCmim.Enabled = true;
            GetId_Tip(this, null);

        }
Posted
Comments
PrashantShirke 9-May-12 6:13am    
at which line ur getting this error?
IviKAZAZI 9-May-12 6:23am    
i = dataGridEksp.SelectedRows[0].Index; here its says the index is out of range.

this error is coming may be because dataGridEksp.SelectedRows is coming null...
so following may help you,

instead of using ..

C#
txt_rrugaEks.Text = dataGridEksp.Rows[i].Cells[0].Value.ToString();


use following...

C#
txt_rrugaEks.Text = dataGridEksp.Rows[e.RowIndex].Cells[0].Value.ToString();


for rest continue like above

OR u can use...

i = e.RowIndex;
 
Share this answer
 
v4
Comments
IviKAZAZI 10-May-12 7:00am    
The problem shows when i select one row at first grid,and one colum at second grid.
i = dataGridEksp.SelectedRows[0].Index; here its says the index is out of range.
This means there is no selected row at all. Make sure that there is/are selected rows to access before accessing them.
 
Share this answer
 
Comments
IviKAZAZI 10-May-12 6:59am    
The problem shows when i select one row at first grid,and one colum at second grid.

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