Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few parts to the answer to your question and I'm making a few assumptions (you're using Visual Studio as your IDE, you can use VB as your code-behind language): </p> <blockquote> <p>I want to bind a sql statement to populate the dropdownlist...</p> </blockquote> <p>You can do this either in code-behind or through the Visual Studio GUI. While you may use a boundfield for the dropdownlist, the templatefield ultimately gives you more flexibility. I'd read up on the templatefield (<a href="http://msdn.microsoft.com/en-us/library/bb288032.aspx" rel="nofollow">here</a>), as it will be your friend using the Gridview for anything beyond basic data display. Using the GUI, selecting the dropdownlist should give you a small arrow to the upper-right that will allow you to create a data connection and datasource to bind to your dropdownlist. </p> <blockquote> <p>Also based on the selection the user makes with this dropdownlist, I want to populate the next column (column2) with the corresponding data</p> </blockquote> <p>This is a little more complex, as you will need to trigger PostBack (using AutoPostBack) on the Dropdownlist, handle the Dropdownlist's SelectedIndexChanged event, find the control to be updated in your second column, and update that control based on the selectedindex/item/value from your Dropdownlist. There are several ways to do this but here's the quickest I've found (using asp.net winforms). I'm using the SQL Adventureworks database, populating a dropdown in a templatefield in column 1 with the employeeID and using the selected employeeID to populate a label in column 2 with that employeeID's managerID. </p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" EnableModelValidation="True" AutoGenerateColumns="False" DataKeyNames="EmployeeID"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" /&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; Select EmployeeID &lt;asp:DropDownList ID="ddEmpID" runat="server" OnSelectedIndexChanged="ddEmpID_SelectedIndexChanged" DataSourceID="SqlDataSource1" DataTextField="EmployeeID" DataValueField="EmployeeID" AutoPostBack="True"&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="labManagerID" runat="server" Text="ManagerID"&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=MyServer\Instance;Initial Catalog=AdventureWorks;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT TOP(10) EmployeeID, ManagerID FROM HumanResources.Employee"&gt; &lt;/asp:SqlDataSource&gt; </code></pre> <p>And the SelectedIndexChanged event from the code-behind: </p> <pre><code>Protected Sub ddEmpID_SelectedIndexChanged(sender As Object, e As EventArgs) Dim myDDList As DropDownList = CType(sender, DropDownList) Dim gvRow As GridViewRow = CType(myDDList.NamingContainer, GridViewRow) Dim myLabel As Label = CType(gvRow.FindControl("labManagerID"), Label) ' Create your sql query here to populate a data object and assign values throughout your row Dim myConnection As SqlConnection = New SqlConnection("Data Source=MyServer\Instance;Initial Catalog=AdventureWorks;Integrated Security=True") Dim myCmd As SqlCommand = New SqlCommand("Select ManagerID FROM HumanResources.Employee WHERE EmployeeID='" + myDDList.SelectedValue + "'", myConnection) If myConnection.State = ConnectionState.Closed Then myConnection.Open() myLabel.Text = myCmd.ExecuteScalar If myConnection.State = ConnectionState.Open Then myConnection.Close() End Sub </code></pre> <p>And since working with the Gridview is sometimes an exercise in self-flagellation, I'd suggest having some good <a href="http://msdn.microsoft.com/en-us/library/aa479339" rel="nofollow">walkthrough tutorials</a> at hand:</p> <p>-J</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload