2012-06-11 2 views
0

Я хотел бы получить имя столбца, которое я нажимаю и значение этой щелкнутой строки.Gridview Row - значение и имя столбца

Я нашел очень хороший пример, но я не могу получить эти данные, которые я хочу.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       e.Row.Attributes["onClick"] = "location.href='view.aspx?id_lekarza=" + DataBinder.Eval(e.Row.DataItem, "Id_lekarza") + " &klikniete=" + "sss" + " '"; 
      } 
     } 

e.Row.RowIndex все время 0?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       e.Row.Attributes.Add("onclick", "javascript:alert('" + e.Row.RowIndex + "');"); 
       e.Row.Style.Add("cursor", "pointer"); 
      } 
     } 

ответ

1

В вашем GridView вы можете добавить поле шаблон, как показано ниже:

<asp:TemplateField ItemStyle-Width="44px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" 
          HeaderStyle-Height="40px" ShowHeader="false"> 
          <ItemTemplate> 
           <asp:ImageButton ID="btnSelect" runat="server" ImageUrl="~/Images/btnSelect.png" 
            CommandName="Select" CommandArgument='<%# Eval("ID") %>'/></ItemTemplate> 
          <HeaderStyle Height="40px"></HeaderStyle> 
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="44px"></ItemStyle> 
</asp:TemplateField> 

И в вашем C# коде следует создать событие RowCommand в Gridview, и получить доступ к CommandArgument этой строки, как следующее :

protected void GV_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
if (e.CommandName == "Select") 
    { 
    //You get the ID of that specific row that you have selected : 
    string ID=e.CommandArgument.ToString(); 
    //Here you write your code !   For example a select query to get that row from database by the ID you have gained in above line of code 
    } 
} 
+0

Можете ли вы привести пример к событию GridView1_RowCreated()? – RPD

+0

Приведенный выше код является примером! ты это сделал? что вы хотите узнать? – Karamafrooz

+0

имя столбца, где была выбрана строка – RPD