简体   繁体   中英

How to Get cell index in gridview

I'm using a website(asp.net,C#) to view some details in gridview. In that gridview I have generated checkboxes dynamically. So it will be placed any cell inside of that gridview. I find that control in grdview by using FindControl(), but I cant get that cell index... now I want to get that excact cell index which placed that checkbox. How shall I get that cell index?

Please anyone Tell me the solution of this problem.

Thanks in advance.

My code for getting that Control is:

if (HeaderCell.Text.Contains(strColumnName))
{
    CheckBox chk = GrdDynamicControls.Rows[index].FindControl(chkCheckBox1.ID) as CheckBox;
    chk.Checked = true;
    strCelValue = chk.Checked.ToString();
}

Try this:

int theCellNumberWhatINeed = -1;
for (int cellNumber = 0; cellNumber < GridView1.Rows[index].Cells.Count; cellNumber++)
{
    foreach (Control ctrl in GridView1.Rows[index].Cells[cellNumber].Controls)
    {
        if (ctrl.ID == "aCheckBox") // or compare by clientid... etc
        {
            theCellNumberWhatINeed = cellNumber;
            break;
        }
    }
}
if (theCellNumberWhatINeed > -1)
{
    // ...
}

Not the most elegant solution but works with the builtin gridview control without creating your own.

You can use Event Args e

e.Item.ItemIndex

it will return an integer

Hope this will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM