简体   繁体   中英

how to get the column index of the gridview

I have two image buttons in the GridView used for selection. They populate the textboxes. What I need is when i click imagebutton "Edit" it should populate the textboxes from the gridview and when the imagebutton "Delete" is pressed it does the same and disbale the textboxes too. The point is am not getting a logic of how to get the column indexes programmatically. help me out with the condition. the issue is just with the c# code. Here is the snippet which i have tried:

  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    var row = GridView1.SelectedRow;

            if (condition)
            {
                _PropertyTitle.Text = row.Cells[1].Text;
                _Address.Text = row.Cells[2].Text;
            }
            else
            {
                _PropertyTitle.Text = row.Cells[1].Text;
                _PropertyTitle.Enabled = false;
                _Address.Text = row.Cells[2].Text;
                _Address.Enabled = false;
            }
      }
}

在此处输入图片说明

It is not very clear from question that what you are trying to achieve.But what i got is you want to write specify both image button click separately.

for that you have to use the RowCommand event.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

}

In this row command event according to CommandName (of button) you can manage the code for two buttons

Refer this link for Rowcommand event row command One more link

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