This is a migrated thread and some comments may be shown as answers.

NeedDataSource Event Handler

2 Answers 175 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shamim
Top achievements
Rank 1
Shamim asked on 25 Aug 2010, 07:45 AM
I am using the following click event to fire the NeedDataSource Event handler:

 

protected void btnRecord_Click(object sender, EventArgs e)

 

{

RadGrid1.NeedDataSource+=

new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);

 

 

 

}

and the method to bind the data is followed:


 

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

 

{

 

Contact curContact = new Contact();

 

curContact.ContactName = TextBox1.Text;

curContact.Title = TextBox2.Text;

curContact.Email = TextBox3.Text;

curContact.Phone = TextBox4.Text;

curContact.Fax = TextBox5.Text;

curContact.ContactType = Session[

"ContactType"].ToString();

 

 

 

DataTable dt = new DataTable();

 

dt.Columns.Add(

"ContactName");

 

dt.Columns.Add(

"Title");

 

dt.Columns.Add(

"Email");

 

dt.Columns.Add(

"Phone");

 

dt.Columns.Add(

"Fax");

 

dt.Columns.Add(

"ContactType");

 

dt.Rows.Add(curContact.ContactName, curContact.Title, curContact.Email, curContact.Phone, curContact.Fax,

curContact.ContactType);

RadGrid1.DataSource = dt;

 

}

But this does not seem to populate the radgrid. Am I missing anything? Any help will be appreciated.


2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Aug 2010, 08:09 AM
Hello

The OnNeedDataSource event fires when a call to the grid's Rebind method takes place. So initially attach the OnNeedDataSource event to grid and set the visibility of grid to "false". Now set the grid as Visible and invoke the Rebind() method, which in turn fires the OnNeedDataSource and populates the grid.


P.S: Also you can check for " !IsPostBack "  in NeedDataSource event, if you do not want to set the Visible property of grid.


-Shinu
0
Shamim
Top achievements
Rank 1
answered on 25 Aug 2010, 08:32 AM
Here's the radgrid:

 

<telerik:RadGrid ID="RadGrid1" runat="server" Visible="false" OnNeedDataSource="RadGrid1_NeedDataSource"

 

 

 

 

Height="100px" Width="600px" >

 

 

 

<MasterTableView AutoGenerateColumns="false" >

 

 

 

<Columns>

 

 

<telerik:GridBoundColumn DataField="ContactName" DataType="System.String"

 

 

HeaderText="Contact Name" ReadOnly="True"

 

 

>

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="Title" DataType="System.String"

 

 

HeaderText="Title" ReadOnly="True"

 

 

>

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="Email" DataType="System.String"

 

 

HeaderText="Email" ReadOnly="True"

 

 

>

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="Phone" DataType="System.String"

 

 

HeaderText="Phone" ReadOnly="True"

 

 

>

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="Fax" DataType="System.String"

 

 

HeaderText="Fax" ReadOnly="True"

 

 

>

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="ContactType" DataType="System.String"

 

 

HeaderText="Contact Type" ReadOnly="True"

 

 

>

 

 

</telerik:GridBoundColumn>

 

 

</Columns>

 

 

 

</MasterTableView>

 

 

<ClientSettings>

 

 

<Scrolling AllowScroll="True" UseStaticHeaders="True" />

 

 

</ClientSettings>

 

 

</telerik:RadGrid>

 


Heres the click event:

 

protected void btnRecord_Click(object sender, EventArgs e)

 

{

 

//RadGrid1.NeedDataSource+=new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);

 

RadGrid1.Visible =

true;

 

RadGrid1.Rebind();

 

 

}


and heres the method:

 

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

 

{

 

Contact curContact = new Contact();

 

curContact.ContactName = TextBox1.Text;

curContact.Title = TextBox2.Text;

curContact.Email = TextBox3.Text;

curContact.Phone = TextBox4.Text;

curContact.Fax = TextBox5.Text;

curContact.ContactType = Session[

"ContactType"].ToString();

 

 

 

DataTable dt = new DataTable();

 

dt.Columns.Add(

"ContactName");

 

dt.Columns.Add(

"Title");

 

dt.Columns.Add(

"Email");

 

dt.Columns.Add(

"Phone");

 

dt.Columns.Add(

"Fax");

 

dt.Columns.Add(

"ContactType");

 

dt.Rows.Add(curContact.ContactName, curContact.Title, curContact.Email, curContact.Phone, curContact.Fax,

curContact.ContactType);

RadGrid1.DataSource = dt;

 

}


This one throws a javascript error.
Tags
Grid
Asked by
Shamim
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Shamim
Top achievements
Rank 1
Share this question
or