2012-07-13 67 views
1

我已經用數據源填充了一個datagridview,我的SQL查詢就在其中。我沒有使用存儲過程或類似的東西。它很好地顯示數據,但我無法使用gridview中的數據。例如,gridview1.selectedRow.cells[0].Text和類似的方法不起作用?有沒有人可以幫助我呢?從數據網格視圖讀取數據

<body> 
<form id="form1" runat="server"> 
<asp:ScriptManager ID="ScriptManager" runat="server" /> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
    <h3>Gridview with Filtering</h3> 
     <div class="GridviewDiv"> 
     <table style="width: 540px" border="0" cellpadding="0" cellspacing="1" class="GridviewTable"> 
      <tr > 
       <td style="width: 40px;"> 
        ID 
       </td> 
       <td style="width: 120px;" > 
        First Name 
       </td> 
       <td style="width: 120px;"> 
        Last Name 
       </td> 
       <td style="width: 130px;"> 
        Department 
       </td> 
       <td style="width: 130px;"> 
        Location 
       </td> 
      </tr> 
      <tr > 
       <td style="width: 40px;"> 
       </td> 
       <td style="width: 120px;"> 
       </td> 
       <td style="width: 120px;"> 
       </td> 
       <td style="width: 130px;"> 
        <asp:DropDownList ID="ddldepartment" DataSourceID="dsPopulateDepartment" AutoPostBack="true" 
         DataValueField="department" runat="server" Width="120px" Font-Size="11px" AppendDataBoundItems="true"> 
         <asp:ListItem Text="All" Value="%"></asp:ListItem> 
        </asp:DropDownList> 
       </td> 
       <td style="width: 130px;"> 
        <asp:DropDownList ID="ddlLocation" DataSourceID="dsPopulateLocation" AutoPostBack="true" 
         DataValueField="location" runat="server" Width="120px" Font-Size="11px" AppendDataBoundItems="true"> 
         <asp:ListItem Text="All" Value="%"></asp:ListItem> 
        </asp:DropDownList> 
       </td> 
      </tr> 
      <tr> 
       <td colspan="5"> 
        <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" AllowPaging="True" 
         AllowSorting="true" DataSourceID="dsGridview" Width="540px" PageSize="10" CssClass="Gridview"> 
         <Columns> 
          <asp:BoundField DataField="id" HeaderText="Sort" SortExpression="id" ItemStyle-Width="40px" 
           ItemStyle-HorizontalAlign="Center" /> 
          <asp:BoundField DataField="FirstName" HeaderText="Sort" SortExpression="FirstName" 
           ItemStyle-Width="120px" /> 
          <asp:BoundField DataField="LastName" HeaderText="Sort" SortExpression="LastName" 
           ItemStyle-Width="120px" /> 
          <asp:BoundField DataField="Department" HeaderText="Sort" SortExpression="Department" 
           ItemStyle-Width="130px" /> 
          <asp:BoundField DataField="Location" HeaderText="Sort" SortExpression="Location" 
           ItemStyle-Width="130px" /> 
         </Columns> 
        </asp:GridView> 
       </td> 
      </tr> 
     </table> 
     </div> 
     <asp:SqlDataSource ID="dsGridview" runat="server" ConnectionString="<%$ ConnectionStrings:EvonetConnectionString %>" 
      SelectCommand="SELECT * FROM [T_Employees]" FilterExpression="Department like '{0}%' 
      and Location like '{1}%'"> 
      <FilterParameters> 
       <asp:ControlParameter Name="Department" ControlID="ddldepartment" PropertyName="SelectedValue" /> 
       <asp:ControlParameter Name="Location" ControlID="ddllocation" PropertyName="SelectedValue" /> 
      </FilterParameters> 
     </asp:SqlDataSource> 
     <asp:SqlDataSource ID="dsPopulateDepartment" runat="server" ConnectionString="<%$ ConnectionStrings:EvonetConnectionString %>" 
      SelectCommand="SELECT DISTINCT Department from [T_Employees]"></asp:SqlDataSource> 
     <asp:SqlDataSource ID="dsPopulateLocation" runat="server" ConnectionString="<%$ ConnectionStrings:EvonetConnectionString %>" 
      SelectCommand="SELECT DISTINCT Location FROM [T_Employees]"></asp:SqlDataSource> 
     </div> 
    </ContentTemplate> 
</asp:UpdatePanel> 
</form> 

+2

請發佈您的代碼.. – 2012-07-13 11:35:04

+0

我將它添加到問題。 – JackBauer 2012-07-13 11:52:45

回答

3

使用的列名

dataGridView.Rows[4].Cells["Name"].Value.ToString(); 
如果您想通過每行然後迭代

foreach (DataGridViewRow row in dataGrid.Rows) 
    { 

     foreach (DataGridViewCell cell in row.Cells) 
     { 
      string value = cell.Value.ToString(); 

     } 
    } 

此外,您還可以執行以下操作來實現什麼你想要

string name = (string)dataGridView1.SelectedRows[0].Cells[0].Value; 
0

您必須使用回發事件和參數才能獲取選定的值。

獲取有關this這是一個關於你的問題

1

教程,如果你使用的DataGrid的SelectionChanged事件的讀取。那麼你可以嘗試它

 
    private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
      { 
       object item = dataGrid1.SelectedItem; 
       txtRegId.Text = (dataGrid1.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text; 
       MessageBox.Show(txtName.Text); 
      } 

+0

這是工作在WPF – Joy 2015-02-04 07:16:52