Hello everyone. I am new to C#. I am trying to code an application that will show a single record in a DetailsView once that record is selected from a GridView. I understand how to create it using ObjectDataSource and SqlDataSource, but I would like to create
it programmactically w/in the C# code. Can anyone help? Thanks.
This is what I have so far. This the connection between the DropDownList control and the GridView control:
using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ...;
namespace ...
{
public partial class ... : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql = "select distinct ... FROM ... order by ...";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataReader dr = command.ExecuteReader();
<asp:BoundField HeaderText="your header text" DataField="(your data field)" SortExpression="(your data field)" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Fields>
</asp:DetailsView>
C# Code Behind file (.aspx.cs):
using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace Your Name Space
{
public partial class your page name : System.Web.UI.Page
{
string conn = ConfigurationManager.ConnectionStrings["your connection string from web.config file"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql = "your sql select statement for your drop down list";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataReader dr = command.ExecuteReader();
ddlCatList.DataSource = dr;
ddlCatList.DataValueField = "your data value field for drop down list";
ddlCatList.DataTextField = "your data text field for drop down list";
ddlCatList.DataBind();
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string sql = "SELECT ... WHERE your drop down list field = '" + DropDownList1.SelectedValue.ToString() + "'";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataAdapter da = new OracleDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string sql = "SELECT ... FROM ... WHERE field name in DataKeyNames field in HTML code file = '" + GridView1.SelectedDataKey.Value.ToString() + "'";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataAdapter da = new OracleDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
tssweb
Member
10 Points
8 Posts
Gridview / Detailsview - Master Details App
Apr 05, 2012 08:16 PM|LINK
Hello everyone. I am new to C#. I am trying to code an application that will show a single record in a DetailsView once that record is selected from a GridView. I understand how to create it using ObjectDataSource and SqlDataSource, but I would like to create it programmactically w/in the C# code. Can anyone help? Thanks.
This is what I have so far. This the connection between the DropDownList control and the GridView control:
using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ...;
namespace ...
{
public partial class ... : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql = "select distinct ... FROM ... order by ...";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataReader dr = command.ExecuteReader();
ddl.DataSource = dr;
ddl.DataValueField = "...";
ddl.DataTextField = "...";
ddl.DataBind();
command.Connection.Close();
command.Connection.Dispose();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string sql = "SELECT ... FROM ... WHERE ... = '" + ddl.SelectedValue.ToString() + "'";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataAdapter da = new OracleDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
GVJobs.DataSource = ds;
GVJobs.DataBind();
}
}
}
}
Bimalvv
Contributor
2346 Points
478 Posts
Re: Gridview / Detailsview - Master Details App
Apr 05, 2012 08:26 PM|LINK
Please see this http://www.codeproject.com/Articles/16779/GridView-DetailsView-Master-Detail-Control
Bimal
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(); } } }tssweb
Member
10 Points
8 Posts
Re: Gridview / Detailsview - Master Details App
Apr 27, 2012 04:57 PM|LINK
HTML Code file (.aspx):
<asp:DropDownList ID="ddlCatList" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" Height="27px" Width="183px" BackColor="Maroon"
Font-Bold="True" Font-Names="Georgia" ForeColor="White"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="" Text="Select ..." />
</asp:DropDownList>
<asp:GridView ID="GVJobs" runat="server" BackColor="Black" BorderColor="#003300" BorderStyle="Solid" BorderWidth="3px" CellPadding="0" GridLines="None" CellSpacing="2" ForeColor="Maroon" HorizontalAlign="Center" Font-Bold="False" AutoGenerateColumns="False" Width="820px"
DataKeyNames="(your data table field)"
AutoGenerateSelectButton="True"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="(your data table field)" >
<ItemTemplate>
<asp:Label ID="YourFieldLabel" runat="server" Text='<%# Eval("(your data table field)") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F0F0F0" ForeColor="Maroon" Font-Size="Small" Wrap="False" />
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Maroon" Font-Bold="True" ForeColor="White" Font-Size="Small" HorizontalAlign="Center" Wrap="False" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="Black" BorderColor="#003300" BorderStyle="Solid" BorderWidth="3px" CellPadding="0" GridLines="None" Height="50px" Width="310px" CellSpacing="2"><FooterStyle BackColor="White" ForeColor="#333333" />
<RowStyle BackColor="White" ForeColor="Maroon" Font-Names="Georgia" Font-Size="Small" />
<FieldHeaderStyle BackColor="Maroon" BorderColor="#003300" BorderStyle="Solid" Font-Bold="True" ForeColor="White" VerticalAlign="Top" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" Wrap="False" />
<EditRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField HeaderText="your header text" DataField="(your data field)" SortExpression="(your data field)" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Fields>
</asp:DetailsView>
C# Code Behind file (.aspx.cs):
using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace Your Name Space
{
public partial class your page name : System.Web.UI.Page
{
string conn = ConfigurationManager.ConnectionStrings["your connection string from web.config file"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql = "your sql select statement for your drop down list";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataReader dr = command.ExecuteReader();
ddlCatList.DataSource = dr;
ddlCatList.DataValueField = "your data value field for drop down list";
ddlCatList.DataTextField = "your data text field for drop down list";
ddlCatList.DataBind();
command.Connection.Close();
command.Connection.Dispose();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string sql = "SELECT ... WHERE your drop down list field = '" + DropDownList1.SelectedValue.ToString() + "'";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataAdapter da = new OracleDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string sql = "SELECT ... FROM ... WHERE field name in DataKeyNames field in HTML code file = '" + GridView1.SelectedDataKey.Value.ToString() + "'";
using (OracleCommand command = new OracleCommand(sql, ConnectionManager.GetConnection()))
{
OracleDataAdapter da = new OracleDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
}
}
}