• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

asp.net 2.0 C# Griddata or Detailview and code behind.

asp.net 2.0 C# Griddata or Detailview and code behind.
I would like to setup Gridview or Detailview on the front end only for format because it is very easy, and use code-behind tech.
to make some "If" statement and make the flow control. How can I do that? I may need some good articles for this.
Or if you can show me working codes, you will get 500pts.

0
riskyricky1972
Asked:
riskyricky1972
  • 6
  • 5
  • 4
  • +1
1 Solution
 
RedKelvinCommented:
0
 
madhevan_pillaiCommented:
Hi,

Sample

aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GridViewList.aspx.vb" Inherits="Database_GridViewList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True"
            DataKeyNames="Id">
            <Columns>
                <asp:BoundField DataField="Id" />
                <asp:BoundField DataField="Name" />
            </Columns>
        </asp:GridView>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px"
            Width="176px">
            <Fields>
                <asp:BoundField DataField="Id" />
                <asp:BoundField DataField="Qul" />
            </Fields>
        </asp:DetailsView>
   
    </div>
    </form>
</body>
</html>



codebehind

Imports System.Data
Partial Class Database_GridViewList
    Inherits System.Web.UI.Page
    Dim ds As DataSet
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        Dim dv As DataView = New DataView(ds.Tables("Child"))
        dv.RowFilter = "Id=" + Me.GridView1.SelectedValue.ToString()
        Me.DetailsView1.DataSource = dv
        Me.DetailsView1.DataBind()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ds = Data()
        If Not Me.IsPostBack Then
            Me.GridView1.DataSource = ds.Tables("Parent")
            Me.GridView1.DataBind()
        End If
    End Sub

    Private Function Data() As DataSet

        Dim dt As New DataTable
        dt.Columns.Add("Id", GetType(Integer))
        dt.Columns.Add("Name", GetType(String))

        dt.Rows.Add(New Object() {1, "aaaa"})
        dt.Rows.Add(New Object() {2, "bbbb"})
        dt.Rows.Add(New Object() {3, "cccc"})
        dt.TableName = "Parent"



        Dim dtc As New DataTable
        dtc.Columns.Add("Id", GetType(Integer))
        dtc.Columns.Add("Qul", GetType(String))

        dtc.Rows.Add(New Object() {1, "aaaa"})
        dtc.Rows.Add(New Object() {2, "bbbb"})
        dtc.Rows.Add(New Object() {2, "bbbb"})
        dtc.TableName = "Child"


        Dim ds As New DataSet()

        ds.Tables.Add(dt)
        ds.Tables.Add(dtc)

        Return ds

    End Function

End Class
0
 
riskyricky1972Author Commented:
Not useful
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
RedKelvinCommented:
What is not useful, and why not?
0
 
riskyricky1972Author Commented:
c# pls
0
 
RedKelvinCommented:
My example above is C#, if you would like to use the example by madhevan_pillai, run it through a converter

http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx
http://www.carlosag.net/Tools/CodeTranslator/Default.aspx
http://www.elegancetech.com/CSVB/CSVB.aspx?src=12
0
 
riskyricky1972Author Commented:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GridViewList.aspx.vb" Inherits="Database_GridViewList" %>

This is called c# code? Are you kidding me?
0
 
RedKelvinCommented:
my mistake, but there is no need to be so rude,

You can help yourself, I am signing out of this post.
0
 
madhevan_pillaiCommented:
Hey do u need C# code for the code behind?
0
 
madhevan_pillaiCommented:
aspx

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="GridViewList.aspx.vb" Inherits="Database_GridViewList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True"
            DataKeyNames="Id">
            <Columns>
                <asp:BoundField DataField="Id" />
                <asp:BoundField DataField="Name" />
            </Columns>
        </asp:GridView>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px"
            Width="176px">
            <Fields>
                <asp:BoundField DataField="Id" />
                <asp:BoundField DataField="Qul" />
            </Fields>
        </asp:DetailsView>
   
    </div>
    </form>
</body>
</html>



Codebehind

using System.Data;
partial class Database_GridViewList : System.Web.UI.Page
{
    DataSet ds;
    protected void GridView1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        DataView dv = new DataView(ds.Tables("Child"));
        dv.RowFilter = "Id=" + this.GridView1.SelectedValue.ToString();
        this.DetailsView1.DataSource = dv;
        this.DetailsView1.DataBind();
    }
   
    protected void Page_Load(object sender, System.EventArgs e)
    {
        ds = Data();
        if (!this.IsPostBack) {
            this.GridView1.DataSource = ds.Tables("Parent");
            this.GridView1.DataBind();
        }
    }
   
    private DataSet Data()
    {
       
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
       
        dt.Rows.Add(new object[] {1, "aaaa"});
        dt.Rows.Add(new object[] {2, "bbbb"});
        dt.Rows.Add(new object[] {3, "cccc"});
        dt.TableName = "Parent";
       
       
       
        DataTable dtc = new DataTable();
        dtc.Columns.Add("Id", typeof(int));
        dtc.Columns.Add("Qul", typeof(string));
       
        dtc.Rows.Add(new object[] {1, "aaaa"});
        dtc.Rows.Add(new object[] {2, "bbbb"});
        dtc.Rows.Add(new object[] {2, "bbbb"});
        dtc.TableName = "Child";
       
       
        DataSet ds = new DataSet();
       
        ds.Tables.Add(dt);
        ds.Tables.Add(dtc);
       
        return ds;
       
    }
   
}

0
 
madhevan_pillaiCommented:
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="GridViewList.aspx.cs" Inherits="Database_GridViewList" %>

0
 
madhevan_pillaiCommented:
i hope this would help you.
0
 
riskyricky1972Author Commented:
But I will involve the stored procedure...
0
 
madhevan_pillaiCommented:
Hi,

you can invoke the stored procedure. The final outcome would be a dataset. so u can follow my sample code
0
 
riskyricky1972Author Commented:
can you show me the completed code?
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now