2010-09-10 149 views
1

我正在使用Gridview控件,並且還在ASP .NET中爲每行添加單選按鈕。 這是我想要完成的事情,但我不知道這應該如何完成。問題是我添加了每個gridview的多元數據行爲。像下面的東西。在Gridview行內添加多行內容

alt text

所以,有些情況下,我必須添加兩行,如例子中的一行。並且,ID通常會是用戶可點擊的單選按鈕。有什麼辦法可以做到這一點?

感謝您的幫助。

+0

你是不是故意說「有兩列」,單選按鈕的用途是什麼? – IrishChieftain 2010-09-10 16:35:21

回答

0

這有點奇怪的要求,但我在過去做過類似的事情,我爲每行的筆記框添加了第二行,因爲它太寬而不適合當前行。

在你RowDataBound事件嘗試這樣的事:

GridView x = (GridView)sender; 

    if (e.Row.RowType == DataControlRowType.DataRow && x.EditIndex == e.Row.RowIndex) 
    { 
     TextBox notes = (TextBox)e.Row.Cells[0].Controls[0]; 
     notes.Height = // some height 
     notes.Width = // some width 
     notes.TextMode = TextBoxMode.MultiLine; 
     e.Row.Cells[0].Controls.Clear(); 
     GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal); 
     TableCell cell = new TableCell(); 
     cell.ColumnSpan = // gridview columns count; 
     cell.Controls.Add(notes); 
     row.Cells.Add(cell); 
     x.Controls[0].Controls.AddAt(x.EditIndex + 2, row); 
    } 

注意,這是抓住從第23列中的現有綁定文本框,並將其複製到一個新行,那麼它從原來的單元格中刪除。此外,便籤盒僅在該行表示正在編輯,因此:&& x.EditIndex == e.Row.RowIndexx.Controls[0].Controls.AddAt(x.EditIndex + 2, row);

你可能只是想new GridViewRownew TableCell部分。

0

對於單選按鈕,您將需要使用

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:RadioButton ID="RadioButton1" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 

根據所要綁定到GridView的數據,例如對象列表中的對象列表,您可以使用Container.DataItem在GridView中綁定Gridview。

<asp:TemplateField> 
    <ItemTemplate>   
     <asp:GridView ID="GridView2" runat="server" DataSource='<%((Relationships)Container.DataItem).People %>'> 
      <Columns>        
      </Columns> 
     </asp:GridView> 
    </ItemTemplate> 
</asp:TemplateField>