Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Participant
888 Points
127 Posts
Apr 10, 2012 08:36 AM|LINK
Hi,
What i understand is "you create a dropdownlist and select an item and then the gridview display the information then select a row
from the gridview and the detailsview display all the details". if i am wrong correct me if i am correct, i think you can write
gridView1_SelectedIndexChanged event, then you write some syntax to get the data from the gridview.
like this:
" string temp1=GridView1.SelectedRow.Cells[1].Text.ToString();
string temp2 = gridView1.SelectedRow.Cells[2].Text.ToString();"
then you can write sqlconnection command to get data to bind with detailsview. Here is my work, the
server is mssql2008 not oracle, it might help.
.aspx file:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <p>dropdownlist</p> <p> <asp:DropDownList runat="server" id="ddl" DataTextField="test" DataValueField="test" onselectedindexchanged="ddl_SelectedIndexChanged" Height="16px" Width="386px" AppendDataBoundItems="True" AutoPostBack="True"> </asp:DropDownList> </p> <p>-------------------------------------------</p> <p>gridview</p> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="488px" onselectedindexchanged="GridView1_SelectedIndexChanged" ShowHeaderWhenEmpty="True" AutoGenerateSelectButton="True" > <Columns> <asp:BoundField DataField="test" HeaderText="test" SortExpression="test" /> <asp:BoundField DataField="pwd" HeaderText="pwd" SortExpression="pwd" /> </Columns> <EditRowStyle BackColor="Black" ForeColor="#66FF33" /> <SelectedRowStyle BackColor="Aqua" ForeColor="#333300" /> </asp:GridView> <br /> -------------------------------------------<br /> detailsview<br /> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px" Width="125px"> <Fields> <asp:BoundField DataField="test" HeaderText="test" SortExpression="test" /> <asp:BoundField DataField="pwd" HeaderText="pwd" SortExpression="pwd" /> </Fields> </asp:DetailsView> </form> </body> </html>
.cs file
using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; namespace T_2012_4_10 { public partial class WebForm2 : System.Web.UI.Page { string connectionString = "Data Source=(local);Initial Catalog=Test_db;Integrated Security=SSPI"; protected void Page_Load(object sender, EventArgs e) { SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectionString; connection.Open(); string queryString = "SELECT [test],[pwd] FROM test"; SqlCommand cmd = connection.CreateCommand(); cmd.CommandText = queryString; SqlDataReader reader = cmd.ExecuteReader(); ddl.DataSource = reader; ddl.DataBind(); connection.Close(); connection.Dispose(); } protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { string sql = "SELECT * FROM test WHERE test='"+ddl.SelectedValue.ToString()+"'"; SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectionString; connection.Open(); SqlCommand cmd = connection.CreateCommand(); cmd.CommandText = sql; SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string temp1=GridView1.SelectedRow.Cells[1].Text.ToString(); string temp2 = GridView1.SelectedRow.Cells[2].Text.ToString(); SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectionString; connection.Open(); string queryString = "SELECT * FROM test WHERE test='" + temp1 + "'" + " and pwd='" + temp2 + "'"; SqlCommand cmd = connection.CreateCommand(); cmd.CommandText = queryString; SqlDataReader reader = cmd.ExecuteReader(); DetailsView1.DataSource = reader; DetailsView1.DataBind(); } } }
Mark-yu
Participant
888 Points
127 Posts
Re: Gridview / Detailsview - Master Details App
Apr 10, 2012 08:36 AM|LINK
Hi,
What i understand is "you create a dropdownlist and select an item and then the gridview display the information then select a row
from the gridview and the detailsview display all the details". if i am wrong correct me if i am correct, i think you can write
gridView1_SelectedIndexChanged event, then you write some syntax to get the data from the gridview.
like this:
" string temp1=GridView1.SelectedRow.Cells[1].Text.ToString();
string temp2 = gridView1.SelectedRow.Cells[2].Text.ToString();"
then you can write sqlconnection command to get data to bind with detailsview. Here is my work, the
server is mssql2008 not oracle, it might help.
.aspx file:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <p>dropdownlist</p> <p> <asp:DropDownList runat="server" id="ddl" DataTextField="test" DataValueField="test" onselectedindexchanged="ddl_SelectedIndexChanged" Height="16px" Width="386px" AppendDataBoundItems="True" AutoPostBack="True"> </asp:DropDownList> </p> <p>-------------------------------------------</p> <p>gridview</p> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="488px" onselectedindexchanged="GridView1_SelectedIndexChanged" ShowHeaderWhenEmpty="True" AutoGenerateSelectButton="True" > <Columns> <asp:BoundField DataField="test" HeaderText="test" SortExpression="test" /> <asp:BoundField DataField="pwd" HeaderText="pwd" SortExpression="pwd" /> </Columns> <EditRowStyle BackColor="Black" ForeColor="#66FF33" /> <SelectedRowStyle BackColor="Aqua" ForeColor="#333300" /> </asp:GridView> <br /> -------------------------------------------<br /> detailsview<br /> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px" Width="125px"> <Fields> <asp:BoundField DataField="test" HeaderText="test" SortExpression="test" /> <asp:BoundField DataField="pwd" HeaderText="pwd" SortExpression="pwd" /> </Fields> </asp:DetailsView> </form> </body> </html>.cs file
using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; namespace T_2012_4_10 { public partial class WebForm2 : System.Web.UI.Page { string connectionString = "Data Source=(local);Initial Catalog=Test_db;Integrated Security=SSPI"; protected void Page_Load(object sender, EventArgs e) { SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectionString; connection.Open(); string queryString = "SELECT [test],[pwd] FROM test"; SqlCommand cmd = connection.CreateCommand(); cmd.CommandText = queryString; SqlDataReader reader = cmd.ExecuteReader(); ddl.DataSource = reader; ddl.DataBind(); connection.Close(); connection.Dispose(); } protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { string sql = "SELECT * FROM test WHERE test='"+ddl.SelectedValue.ToString()+"'"; SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectionString; connection.Open(); SqlCommand cmd = connection.CreateCommand(); cmd.CommandText = sql; SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string temp1=GridView1.SelectedRow.Cells[1].Text.ToString(); string temp2 = GridView1.SelectedRow.Cells[2].Text.ToString(); SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectionString; connection.Open(); string queryString = "SELECT * FROM test WHERE test='" + temp1 + "'" + " and pwd='" + temp2 + "'"; SqlCommand cmd = connection.CreateCommand(); cmd.CommandText = queryString; SqlDataReader reader = cmd.ExecuteReader(); DetailsView1.DataSource = reader; DetailsView1.DataBind(); } } }