Showing posts with label Details View. Show all posts
Showing posts with label Details View. Show all posts

December 14, 2011

Details View Row Invisible from Code Behind

Below is the sample code if you want to make some rows invisible in details view

//Method to Bind details View from DB

 public void GetUserProfile()
    {
        DataSet ds = sqlobj.RunQueryReturnDataSetonLoginDB("exec [GetServiceUsage] '" + Session["UserId"].ToString() + "'" + "," + 0);
        if (ds != null)
        {
            if (ds.Tables[0] != null) 
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dView.DataSource = ds.Tables[0];
                    dView.DataBind();


                }


            }


        }




    }


//Make Specific rows which have cell text ="0" invisible

    if (dView != null)
            {
                DataRowView row = dView.DataItem as DataRowView;
                
                    if (row  != null)
                    {
                       //Row 1
                        if (row["Testrow"].ToString() == "0" )
                        {
                            dView.Rows[0].Visible = false;


                        }


                      //Row 2
                        if (row["Testrow2"].ToString() == "0" )
                        {
                            dView.Rows[1].Visible = false;


                        }


                    }
                
            }


June 25, 2011

Code to Populate Selected Row in Details View From ASP.NET Grid View


Example11 .aspx
<%@ Page Language="C#" ErrorPage ="~/Error.aspx" AutoEventWireup="true" CodeFile="Example11.aspx.cs" Inherits="Example11" %>


<!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" 
              OnSelectedIndexChanging="GridView1_SelectedIndexChanging" 
            AutoGenerateSelectButton="true" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
        </asp:GridView>
        <asp:DetailsView ID="DetailsView1"  runat="server" Height="50px" Width="125px">
        </asp:DetailsView>
    </div>   
    </form>
</body>
</html>



Example11 .aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using NLog;


public partial class Example11 : System.Web.UI.Page
{
    SqlConnection connObj = new SqlConnection();
    DataSet ds;
    SqlCommand objCommand;
    SqlDataAdapter objAdapter;


    protected void Page_Load(object sender, EventArgs e)     
    {
        connObj.ConnectionString = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ToString();

//Bind Data To grid View
            connObj.Open();
            ds = new DataSet();
            objCommand = new SqlCommand("Select * from student", connObj);
            objAdapter = new SqlDataAdapter(objCommand);
            objAdapter.Fill(ds);
            connObj.Close();


            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
    }

    protected void GridView1_SelectedIndexChanging(object sender, EventArgs e)
    {
     
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        int id =int.Parse ( row.Cells[1].Text );

//Bind selected row from  grid View to Details View                connObj.Open();
        ds = new DataSet();
        objCommand = new SqlCommand("Select * from student where Student_ID= " + id, connObj);
        objAdapter = new SqlDataAdapter(objCommand);
        objAdapter.Fill(ds);
        connObj.Close();


        DetailsView1 .DataSource = ds.Tables[0];
        DetailsView1.DataBind();


    }
}



Below is the Connection String in web.config file.


<add name="DotNetTrainingConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=DotNetTraining;Integrated Security=True" providerName="System.Data.SqlClient"/>

See the below o/p on 10 as selected row from the Grid