views:

25

answers:

1

i try to make myGridView Companent via using WebPArt you know it :

using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.WebControls; 
using System.Web.UI;

namespace MyGridView
{
    public class MyGridView : WebPart
    {
        GridView gv;

        protected override void CreateChildControls()
        {
            gv = new GridView();
            gv.CssClass = "tablestyle";
            this.Controls.Add(gv);
        }

    }
}

i added this GridView on Toolbox. Every Thing is ok . İ want to add my own CSS design to my GridView. But if i drow this gridView from toolbox to aspx page. if i bind my datasource ; Datasource is not show himself.

Like that Main program:


protected void Page_Load(object sender, EventArgs e)
        {
          LoadData loaddata = new LoadData();
          DataTable  dt = loaddata.LoadSQL("conn", "sp_GetAllCategory");
           MyGridView1.datas....   -----> i can not see DataSource why?
        }

i want to see My GridView DataSource. if i write Binding datasource. MyGridView1.DataSource -----> i can not see DataSource why?

A: 

Hey,

Because you need to add DataSource to the control; web part doesn't support it by default (basedataboundcontrol class defines DataSource and DataBind). So you need to add this:

public object DataSource
{
   get 
   {
       this.EnsureChildControls();
       return gv.DataSource; 
   }
   set 
   { 
       this.EnsureChildControls();
       gv.DataSource = value; 
   }
}

Typically, you must call EnsureChildControls() so that all child controls are created before you wrap the grid's properties, but I'm not sure if EnsureChildControls is accessible to you. I think it is.

Brian
gv.DataSource = value; give me error : Object reference not set to an instance of an object.
Phsika
protected override void CreateChildControls() { gv = new GridView(); gv.CssClass = "tablestyle"; this.Controls.Add(gv); } public object DataSource { get { return gv.DataSource; } set { gv.DataSource = value; } }is not working...
Phsika
i try do it : protected void Page_Load(object sender, EventArgs e) { LoadData loaddata = new LoadData(); MyGridView1.DataSource = loaddata.LoadSQL("conn", "sp_GetAllCategory"); if (!IsPostBack) { MyGridView1.DataBind(); } }
Phsika
Updated for the first part.
Brian
Also, in page_load, you would also need to call EnsureChildControls within your custom control to ensure the grid is created in createdhildcontrols.
Brian
Thanks Brain; But there is another problem: how to connect stylesheet.css file in mygridview.cs. i mean ; i added gvCustomers.CssClass = "tablestyle"; but myGridView can not connect Stylesheet.css. How can i do that?
Phsika
Well, you could let the page/master page be responsible for referencing the stylesheet.css file, and expose in your custom control a GridCssClass property which passes this to the CssClass of the grid... if that is what you are doing, then maybe the CSS style is being applied incorrectly. Run the page, view source, and see where it applies the style. That would be the easiest way; you can also make it an embedded resource and append it to the page's HEAD section in the Load event too.
Brian