Как я могу получить значение выбранной ячейки строки вида сетки на странице? - PullRequest
8 голосов
/ 23 мая 2011

Я использую сетку на моей странице.

Я хочу показать данные выбранной ячейки строки через response.write(), по событию нажатия кнопки страницы.

Ответы [ 5 ]

8 голосов
/ 23 мая 2011

Примечание ::

  • пожалуйста, установите CommandName вашего кнопка "selectCol"

  • Пожалуйста, установите CommandName для вторая кнопка, которую вы будете использовать для удалить

    до "deleteCol"

Установите свойство command argument для своей кнопки:

.aspx

CommandArgument='<%#((GridViewRow)Container).RowIndex%>'
CommandArgument='<%#((GridViewRow)Container).RowIndex%>'

для двух кнопок.

.cs

 protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int index = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "selectCol")
            {
                Response.Write(gv.Rows[index].Cells[0].Text); //consider you use bound field and the column you want to show its value is the first column.
            }

            else if(e.CommandName == "deleteCol")
             {
                 int id = int.Parse(gv.DataKeys[index].Value.ToString());//the primary key for your table.
                 Delete(id);//method which use (Delete From .... Where id = ....).
             }


            gv.DataBind();

        }

        catch (Exception ee)
        {
            string message = ee.Message;
        }
    }
4 голосов
/ 16 октября 2012

Приветствие Его.

Самый простой способ прочитать значение из поля gridview - написать:
your_grid_name.SelectedRow.Cell (* number_of_index *). Text

В моем случае это:

Dim Employer_name As String
Employer_name = poslodavac_grid.SelectedRow.Cells (1) .Текст

Просто помните, что индекс первой ячейки равен нулю, и он не считается тегом «asp: CommandField ShowSelectButton» как первый ...

3 голосов
/ 17 ноября 2012

Если вы используете LINK BUTTON в сетке, вы можете использовать следующий код в методе ROWCOMMAND.Этот код извлекает все значения в выбранной строке.

 // to get the value of the link use the command argument 
 FaultId = Convert.ToInt32(e.CommandArgument);

 // to get the other column values 

UserId = Convert.ToInt32(((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[1].Text);  

Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;

ProblemType = ((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer)).Cells[3].Text;
3 голосов
/ 23 мая 2011

Использование GridView.SelectedRow свойство.

String cellText = this.gridView.SelectedRow.Cells[cellIndex].Text;

Обратитесь к следующему, чтобы узнать о выборе строки в GridView элементе управления.

Выбрать команду в элементе управления GridView в ASP.Net

2 голосов
/ 23 мая 2011

Вы можете получить его в событии RowCommand вида сетки:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{
    if (e.CommandName == "Select")
    {
        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
        Response.Write(row.Cells[0].Text);
        Response.Write(row.Cells[1].Text);
        ................
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...