2012-12-10 26 views
1

我有一個GridView與用戶,我希望能夠選擇一行的第一列 - 這是與用戶名。我知道如何用autogenerateselectbutton屬性來做到這一點,但它在我的設計上看起來不太好。相反,我想讓它沒有選擇按鈕。以下是我迄今爲止:從GridView asp.net中選擇一個單元值c#

<asp:GridView ID="GridView5" AllowPaging="true" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle="alt" runat="server" AllowSorting="True" 
          AutoGenerateColumns="False" 
          Width="750px" 
          CausesValidation="False" OnPageIndexChanging="gridView5_PageIndexChanging" OnSelectedIndexChanged="gridView5_SelectedIndexChanged" autogenerateselectbutton="True" > 
          <Columns> 
           <asp:BoundField DataField="username" HeaderText="Username" ReadOnly="True" /> 
           <asp:BoundField DataField="name" HeaderText="Name" /> 
           <asp:BoundField DataField="lastname" HeaderText="Last name" /> 
          </Columns> 
          <selectedrowstyle backcolor="LightCyan" 
    forecolor="DarkBlue" 
    font-bold="true"/> </asp:GridView> 

如果我刪除autogenerateselectbutton,然後在一排點擊什麼也不做。

這是我的C#代碼,它從這個單元格中獲取值並將其存儲到一個字符串中,然後我使用單元格值作爲參數調用setUser()函數。

protected void gridView5_SelectedIndexChanged(Object sender, EventArgs e) 
{ 
    GridViewRow row = GridView5.SelectedRow; 
    string user = row.Cells[1].Text.ToString(); 
    setSelectedUser(user); 
} 

我認爲這個問題是在aspx文件,其中創建我GridView,但不知道如何讓選擇該行沒有autogenerateselectbutton設置爲true。

+0

你有沒有調試的C#代碼,並試圖找到如果'字符串user'值爲null或不是? –

+0

是的,我做到了,如果我刪除'autogenerateselectbutton'然後它是空的,但然後我t不可能選擇一行。 – Apostrofix

+0

以下任何答案是否適合您? –

回答

2

你想用你可以創建一個ASP的RowCommand方法:ButtonField字段列,設置命令名

protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) 
    { 
     if (e.CommandName=="MyCommand") 
     {  
      int row = int.Parse(e.CommandArgument.ToString()); 
      var cellText= gvOwner.Rows[row].Cells[1].Text.Trim(); 
     } 
    } 
+0

** FYI **:他使用GridView.SelectedIndexChanged事件 –

+1

如果你想選擇一個沒有按鈕的行,並且只需要點擊這裏的id就可以解決方案:[Here](http://www.codeproject.com/Articles)/15898 /在網格中選擇一個點擊行) – GeorgesD

+0

@FettMo但是,我該如何設置這個ButtonField從我的數據庫中獲取結果,這是用戶名。使用BoundField,我可以使用'DataField =「username」',然後如何調用'RowCommand'? – Apostrofix

0

看起來不錯,但您的網頁postbacking選擇後?如果(!ispostback),你檢查回發控制 ,因爲當頁面回發時,gridview也會,這意味着grv將是空的。如何從空grv中獲取數據你有使用回發控制你的數據源和數據綁定行...

+0

是的,但我現在沒有做其他任何事情。只需將該單元格的值存儲到'setSelectedUser'函數中即可。這個想法是,點擊一個按鈕後,我仍然可以使用選定的用戶。 – Apostrofix

0

把選擇文本,命令名稱作爲「選擇」並傳遞「用戶名」作爲命令參數linkbutton。

<asp:LinkButton runat="server" ID="lnkStatus" CommandName="Select" 
          CommandArgument='<%# Eval("username") %>' Text='Select' /> 

現在啓動RowCommand事件: - 從參數,獲取用戶名,無需從電網得到它。

1

,如果你想獲得一個特定行的選擇索引值,其中,如果你想獲得一個數值從標籤控件,然後」

用戶你必須得到的rowIndex值 任何operation.then
Label l=(Label)Gridviewname.rows[e.rowindex].Findcontrol("label name of which you want to get the data") 
String s=l.Text; 
setselecteduser(l); 

,如果你想從TextFiled獲得價值

TextField t=(TextField)GridviewName.rows[e.rowindex].FindControl("Textbox Id of which you want to get the data") 
String s=t.Text; 
SetSelecteduser(t); 
+0

這不是我所需要的。我已經這樣做了。 – Apostrofix

+0

「RowIndex」屬性將獲得用戶選擇的整個行。您可以準確解釋您需要的配對嗎? –

+0

選擇不起作用 - 例如當我將鼠標移到要選擇的單元格上時,單擊它時什麼都不會發生。否則,獲取RowIndex和單元格的代碼是正確的。 – Apostrofix

相關問題