PDA

View Full Version : [CLOSED] Creating Store and GridPanel Dynamically



Tonic
Mar 28, 2013, 8:39 AM
Hi there

I am trying to make store and GridPanel Dynamically on Page_Load which is working fine Below is my Code
in the below code i call a "bindstore()" function which builds the store and inside "bindstore()" i call a function "BuildGridPanel()" which builds
columnModel and columns in the gridpanel.


.ASPX


*<ext:Store ID="strReportViewer" runat="server">
* * * * * * * * </ext:Store>
* * * * * * * * <ext:Container ID="Container1" runat="server" Region="East">
* * * * * * * * * * <Items>
* * * * * * * * * * * * <ext:Panel ID="Panel1" runat="server" Region="West" Title="Reports" Width="1001">
* * * * * * * * * * * * * * <Items>
* * * * * * * * * * * * * * * * <ext:GridPanel ID="gpReportViewer" EnableViewState="true" Width="1000" runat="server">
* * * * * * * * * * * * * * * * </ext:GridPanel>
* * * * * * * * * * * * * * </Items>
* * * * * * * * * * * * </ext:Panel>
* * * * * * * * * * </Items>
* * * * * * * * </ext:Container>


.ASPX.CS




protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
bindStore();

}

}

private void bindStore()
{
DataSet dsRecordFindRecord = new DataSet();
DataSet finaldataset = new DataSet();
if (Repid == "" || Repid == null)
{
finaldataset = fn.Selectdata(fn.Selectsinglevalues("select repReportSQL from tc_ReportCatalogue where pk_ReportId = '8'"));

}
else
{
finaldataset = fn.Selectdata(fn.Selectsinglevalues("select repReportSQL from tc_ReportCatalogue where pk_ReportId = '" + Repid + "'"));

}
// Ext.Net.Store Store1 = new Ext.Net.Store();
strReportViewer.DataSource = finaldataset;
//Store1.GroupField = "empLocation";
Ext.Net.Model storereader = new Ext.Net.Model();
//storereader.IDProperty = "empNo";

for (int c = 0; c < finaldataset.Tables[0].Columns.Count; c++)
{
Ext.Net.ModelField rf = new Ext.Net.ModelField();
rf.Name = finaldataset.Tables[0].Columns[c].ColumnName;
storereader.Fields.Add(rf);
}


strReportViewer.Model.Add(storereader);
strReportViewer.DataBind();
BuildGridPanel(finaldataset);
gpReportViewer.Store.Add(strReportViewer);
gpReportViewer.DataBind();
}



public void BuildGridPanel(DataSet ds)
{

gpReportViewer.Width = Unit.Pixel(1000);
gpReportViewer.Height = Unit.Pixel(500);
DataSet dsgetcol = ds;
//////XXXXXXXXXXXXXX///////////
////For Fetching columns display name///
string columns = "";
for (int chk = 0; chk < dsgetcol.Tables[0].Columns.Count; chk++)
{

columns = columns + dsgetcol.Tables[0].Columns[chk].ColumnName + ",";
//gpReportViewer.ColumnModel.Columns.RemoveAt(chk);
}
columns.Remove(columns.Length - 1);
string[] colname = columns.Split(',');
////for adding column model to gridpanel

for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
Ext.Net.Column col = new Ext.Net.Column();
col.ID = ds.Tables[0].Columns[i].ColumnName;
col.Text = colname[i];
col.DataIndex = ds.Tables[0].Columns[i].ColumnName;
col.Align = Ext.Net.Alignment.Left;
col.Sortable = true;
//col.Groupable = true;
//col.Lockable = true;

//col.SummaryType = Ext.Net.SummaryType.Average;
gpReportViewer.ColumnModel.Columns.Add(col);
}
}



It All Works Fine at the first time but when am trying to rebind every thing on click of a treePanel Node
its not working below is the code of TreeNode Click

.aspx (DirectEvet Tag Inside TreePanel)





<DirectEvents>
<ItemClick OnEvent="Refresh_Grid" Before="if(!record.data.leaf){return false;};">
<ExtraParams>
<ext:Parameter Name="id" Value="record.data.id" Mode="Raw" />
</ExtraParams>
</ItemClick>
</DirectEvents>



.aspx.cs



protected void Refresh_Grid(object sender, DirectEventArgs e)
{
string Id = e.ExtraParams["id"].ToString();
Repid = e.ExtraParams["id"].ToString();
bindStore();


}



Please help


Thanks in advance

Daniil
Mar 28, 2013, 10:33 AM
Hi Tonic,

Do you need to bind new data only? Or do you also need to reconfigure the GridPanel (e.g. remove/add columns)?

Tonic
Apr 01, 2013, 6:20 AM
Hi Tonic,

Do you need to bind new data only? Or do you also need to reconfigure the GridPanel (e.g. remove/add columns)?

Only new Data No Remove or add

Daniil
Apr 01, 2013, 7:13 AM
Do you need to change a Store's Model?

Tonic
Apr 01, 2013, 7:23 AM
Do you need to change a Store's Model?

yes i need to change model on each request

Daniil
Apr 01, 2013, 7:27 AM
Well, it means that you need to change Columns, doesn't? Just you said that you don't need to add/remove Columns. It is confusing.

Tonic
Apr 01, 2013, 7:35 AM
Well, it means that you need to change Columns, doesn't? Just you said that you don't need to add/remove Columns. It is confusing.


Oh sorry I thought you are asking for giving facility to user at the run time to add new record or delete record with a click of a button or something

let me clarify this

on each request i rebind the store (change the Model) , and then regenerate the Columns as per the model.

Daniil
Apr 01, 2013, 8:09 AM
Ok, thank you for clarifying.

According to your requirement it would be simplest to re-render the GridPanel.

Your Refresh_Grid DirectEvent handler should work if you do the following changes.

1. Add this to the end of the BuildGridPanel method.

if (X.IsAjaxRequest)
{
gpReportViewer.Render();
}

2. Set up some ID explicitly for a GridPanel (the same for each request). The Render method will use it to remove a previously rendered GridPanel.

Tonic
Apr 01, 2013, 8:20 AM
Ok, thank you for clarifying.

According to your requirement it would be simplest to re-render the GridPanel.

Your Refresh_Grid DirectEvent handler should work if you do the following changes.

1. Add this to the end of the BuildGridPanel method.

if (X.IsAjaxRequest)
{
gpReportViewer.Render();
}

2. Set up some ID explicitly for a GridPanel (the same for each request). The Render method will use it to remove a previously rendered GridPanel.


Can you please clarify your second point further

Daniil
Apr 01, 2013, 8:40 AM
The Render method generates a piece of JavaScript to render a GridPanel. Before creating a GridPanel it destroys a component with a GridPanel's ID to avoid ids conflict.

Tonic
Apr 01, 2013, 9:51 AM
The Render method generates a piece of JavaScript to render a GridPanel. Before creating a GridPanel it destroys a component with a GridPanel's ID to avoid ids conflict.

Danil,

Problem is still there i added the below code part in my function


if (X.IsAjaxRequest)
{
gpReportViewer.ID = "gvr";
gpReportViewer.Render();
}




another thing i just noticed is that when i remove my bindstore() function from Page_Load GridPanel is not Created. see Below Code




protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
bindStore();

}


private void bindStore()
{
DataSet dsRecordFindRecord = new DataSet();
DataSet finaldataset = new DataSet();
if (Repid == "" || Repid == null)
{
finaldataset = fn.Selectdata(fn.Selectsinglevalues("select repReportSQL from tc_ReportCatalogue where pk_ReportId = '2'"));

}
else
{
finaldataset = fn.Selectdata(fn.Selectsinglevalues("select repReportSQL from tc_ReportCatalogue where pk_ReportId = '" + Repid + "'"));

}

strReportViewer.DataSource = finaldataset;

Ext.Net.Model storereader = new Ext.Net.Model();


for (int c = 0; c < finaldataset.Tables[0].Columns.Count; c++)
{
Ext.Net.ModelField rf = new Ext.Net.ModelField();
rf.Name = finaldataset.Tables[0].Columns[c].ColumnName;
storereader.Fields.Add(rf);
}


strReportViewer.Model.Add(storereader);
strReportViewer.DataBind();
BuildGridPanel(finaldataset);
gpReportViewer.Store.Add(strReportViewer);
gpReportViewer.DataBind();
}



public void BuildGridPanel(DataSet ds)
{

gpReportViewer.Width = Unit.Pixel(1000);
gpReportViewer.Height = Unit.Pixel(400);
DataSet dsgetcol = ds;
//////XXXXXXXXXXXXXX///////////
////For Fetching columns display name///
string columns = "";
for (int chk = 0; chk < dsgetcol.Tables[0].Columns.Count; chk++)
{

columns = columns + dsgetcol.Tables[0].Columns[chk].ColumnName + ",";
//gpReportViewer.ColumnModel.Columns.RemoveAt(chk);
}
columns.Remove(columns.Length - 1);
string[] colname = columns.Split(',');
////for adding column model to gridpanel

for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
Ext.Net.Column col = new Ext.Net.Column();
col.ID = ds.Tables[0].Columns[i].ColumnName;
col.Text = colname[i];
col.DataIndex = ds.Tables[0].Columns[i].ColumnName;
col.Align = Ext.Net.Alignment.Left;
col.Sortable = true;


gpReportViewer.ColumnModel.Columns.Add(col);
}
if (X.IsAjaxRequest)
{
gpReportViewer.ID = "gvr";
gpReportViewer.Render();
}
}




if i remove my function bindstore() from Page_Load nothing Works

Daniil
Apr 01, 2013, 9:57 AM
You should set up

gpReportViewer.ID = "gvr";
for an each grid including the one that you created during an initial Page_Load.

And you should not remove the bindStore from the Page_Load.

Tonic
Apr 01, 2013, 11:09 AM
You should set up

gpReportViewer.ID = "gvr";
for an each grid including the one that you created during an initial Page_Load.

And you should not remove the bindStore from the Page_Load.

Ok As per your sugeestion i have made the changes with some modification

Now the gridpanel is rendring



gpReportViewer.ID = "gvr";
if (X.IsAjaxRequest)
{

gpReportViewer.Render(true);
}




thanks

Daniil

Daniil
Apr 01, 2013, 11:36 AM
Excellent. So, can we mark the thread as closed?

Tonic
Apr 02, 2013, 10:16 AM
Excellent. So, can we mark the thread as closed?

yes you can