hii all i am trying gridview control in my application.code is executing without errors.but i am not getting output means(internet explorer is coming blank).

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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 System.Xml.Linq;
using System.Data.SqlClient;

public partial class GridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      PopulateGrid();
    }
 
    public void PopulateGrid()
    {
      DataSet objDs = new DataSet();
      String ConnectionString = "server=iconn;database=db_test;user id=sa;password=iconn;";
      SqlConnection Conn = new SqlConnection(ConnectionString);
      System.Data.SqlClient.SqlDataAdapter myCmd;
      myCmd = new System.Data.SqlClient.SqlDataAdapter("StoredProcedure1", Conn);
      myCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
      Conn.Open();
      myCmd.Fill(objDs);
      GridView1.DataSource = objDs;
      GridView1.DataBind();
      //Conn.Close();
    }
 
    public void ItemDataBoundEventHandler1(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='Silver'");
        e.Row.Attributes.Add("onmouseout","this.style.backgroundColor='#FFFBD6'");
      }
    }
}
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="ItemDataBoundEventHandler1">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
Create PROCEDURE StoredProcedure1  
  
AS  
  
      select * from tbl_dtGrid  
  
      RETURN

i have taken the table as tbl_dtGrid with two columns as id and name

Recommended Answers

All 3 Replies

Try,

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

thanks adatapost....now problem is solved
can u please describe why you added Tables[0]

>can u please describe why you added Tables[0]

There are two solutions: If DataSource of GridView is Dataset then you have to set DataMember (tableName) property.

GridView1.DataSource=objDs;
GridView1.DataMember="TableName";
GridView1.DataBind();

Or

If you don't know the exact name datamember then use DataTable instance. objDs.Tables[0] return the reference of DataTable object.

GridView1.DataSource = objDs.Tables[0];
 GridView1.DataBind();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.