cannot bind the ddl in my editable gridview

Posted by LucBite under ASP.NET on 9/14/2009 | Views : 2138 | Status : [Member] | Replies : 22
I have a dropdownlist within my editable gridview.The datasource of the gridview is a datatable method which accepts as a parameter the text of the lable of one of the columns in the gridview.I'm using the code below to try this task but it is not working.

protected void gvServices_DataBinding(object sender, GridViewRowEventArgs e)
{//gvServices is a gridview name
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.Cells[3].FindControl("DDLServType");
Label lbl = (Label)e.Row.Cells[2].FindControl("lblServiceName");
string servicename= lbl.Text;
ddl.DataSource = st.getServiceTypes(servicename);
ddl.DataBind();
}
}




Responses

Posted by: perfectchourasia-9163 on: 4/3/2012 [Member] Starter | Points: 25

Up
0
Down
http://sandeep-chourasia.blogspot.in/2011/09/editable-gridview.html

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/14/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day LucBite

What are you doing here

ddl.DataSource = st.getServiceTypes(servicename);


if you say you are binding the ddl from Datatable , then that line is not supposed to be there. This is Simple Fill the Datatable with Data and Bind the DDL.

if you have problem , we are still here

Thank you for contacting Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: MediaMan on: 9/14/2009 [Member] Starter

Up
0
Down
Vuyiswa,
I have a BLayer class ServiceType with a method returning a datatable and accepting servicename as a parameter. The servicename is the text of the label inside the gridview. So any idea or suggestion, where I should bind it?

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/14/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
First Bind what is Returned from your BLL in a Datatable. That means your come should look like this

Datatable dt = new Datatable();

dt = st.getServiceTypes(servicename);
ddl.Datasource = dt;
ddl.DataTextField= "YourField";
ddl.DataBind();



Tell me if you still have a Problem

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: MediaMan on: 9/14/2009 [Member] Starter

Up
0
Down
Vuyiswa Pls don't be confused we are working together with LucBite

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/14/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
its ok . Did this Solve your Problem ?

Thank you for posting at Dotnetfunda
[Administrator]

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/15/2009 [Member] [MVP] Bronze

Up
0
Down
Hi,

Is there an error being thrown such as a null reference error?

Are you finding a value to pass into your getServiceTypes()?



LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: MediaMan on: 9/15/2009 [Member] Starter

Up
0
Down
No there is no null reference error please check the the word document attached ...to see the problem thanks in advance
 Download source file

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/15/2009 [Member] [MVP] Bronze

Up
0
Down
Hey,

OK I have a new idea - if you are trying to do this in DataBinding then you are probably checking the value of lblServiceName before it has been databound.

In this case you would find the label without an error but it would be asking st.getServiceTypes() for nothing.

Try putting a breakpoint on this line:

ddl.DataSource = st.getServiceTypes(servicename); 


and inspect the value of servicename.

If its empty then you need to try to do this ddl databinding code in the Gridview DataBound event instead.

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: LucBite on: 9/15/2009 [Member] Starter

Up
0
Down
Rtpharry ,I can't see the value. I mean it not even null.

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: LucBite on: 9/15/2009 [Member] Starter

Up
0
Down
Did you mean DataBound or RowDataBound

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/15/2009 [Member] [MVP] Bronze

Up
0
Down
I would say RowDataBound. I think if you look on the MSDN documentation you will also find an additional check to perform.

Firstly check that the rowtype is a data row and secondly check that the row mode is set to edit.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

hmm I cant find anything specific about checking the edit mode in the docs - I could be thinking of one of the other databound controls...

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: LucBite on: 9/17/2009 [Member] Starter

Up
0
Down
Hi Rtpharry , I'm getting this error

"Object reference not set to an instance of an object."
on this line;
"ddlServiceType.Items.Add(st.getServiceTypes(lblServicename.Text).Rows[i][0].ToString()); "

and I have this code

protected void gvServices_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblServicename = (Label)e.Row.FindControl("lblServiceName");
DropDownList ddlServiceType = (DropDownList)e.Row.FindControl("DDLServType");
for (int i = 0; i < st.getServiceTypes(lblServicename.Text).Rows.Count; i++)
{
ddlServiceType.Items.Add(st.getServiceTypes(lblServicename.Text).Rows[i][0].ToString());
}
}
}

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/17/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Which line is it Pointing to ?

Thank you for posting at Dotnetfunda
[Administrator]

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: LucBite on: 9/17/2009 [Member] Starter

Up
0
Down
Oh, sorry Its pointing on this one "for (int i = 0; i < st.getServiceTypes(lblServicename.Text).Rows.Count; i++)"


LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/17/2009 [Member] [MVP] Bronze

Up
0
Down
It seems that your lblServicename is still empty when you are checking it then and that is causing getServiceTypes to return 0 rows?

You need to set a breakpoint on that line, step through it and see what is happening....

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: LucBite on: 9/17/2009 [Member] Starter

Up
0
Down
We have tried to put a break point into the method and the value of service name is null ......have you got any suggestions on what we can do to get a value passed into lbl?.....

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/18/2009 [Member] [MVP] Bronze

Up
0
Down
Ok with reference to that extra check I was talking about earlier this week... I found some code:

if ((e.Row.RowType == DataControlRowType.DataRow)

&& (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{

}


this checks the that the gridview is not in edit mode when you try to find the label.

if you let it get all the way - is there always a value being bound to the lblservicename? is it ever null?

I mean could it be all working correctly but its getting collecting an empty service type?



LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/28/2009 [Member] [MVP] Bronze

Up
0
Down
LucBite, MediaMan - did you ever try my code? Is this problem solved now?

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: LucBite on: 9/29/2009 [Member] Starter

Up
0
Down
Rtpharry , we are so sorry we didn't get back to you. The frustrations our project are running away with us. Well, we did try your code but unfortunately it didn't work....we are still getting an empty DDL.
Thanks for your consent. Any help is still welcome.

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/29/2009 [Member] [MVP] Bronze

Up
0
Down
Hmm its a tricky one! If you are willing to send me a copy of the site and database I can try and get it set up on my computer and see if I can fix the problem...

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: LucBite on: 9/30/2009 [Member] Starter

Up
0
Down
Ok, Rtpharry. we'll arranged it for you.

LucBite, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response