Click here to Skip to main content
15,890,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a dropdown list it contains bookid and book name,i have binded it inside the grid view in row databound control,now i want to save the data that requires book id bt during save there will be no access of that book id from drop down that contains inside the grid view control.how it can be solved

C#
protected void gvStock_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label ddlBookId = (Label)e.Row.FindControl("lblId");
                DropDownList ddlBookName = (DropDownList)e.Row.FindControl("ddlBookName");
                Exception ex;
                DataTable dtblBookName = new DataTable();
                BllBookMaster obj = new BllBookMaster();
                switch (obj.GetAllBookNames(ref dtblBookName, out ex))
                {
                    case 1:
                        DataRow dr = dtblBookName.NewRow();
                        dr["bookid"] = "-1";
                        dr["bookname"] = "Select";
                        dtblBookName.Rows.InsertAt(dr, 0);
                        ddlBookName.DataValueField = "bookid";
                        ddlBookName.DataTextField = "bookname";
                        ddlBookName.DataSource = dtblBookName;
                        ddlBookName.DataBind();
                      
                        break;
                    case -1:
                        lblMsg.Text = ex.Message.ToString();
                        break;
                    case 0:
                       
                        lblMsg.Text = "error";
                        break;

                }
              
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message.ToString();
        }
    }
Posted
Updated 30-Aug-12 21:38pm
v2
Comments
Zoltán Zörgő 31-Aug-12 3:29am    
Language?
__TR__ 31-Aug-12 3:29am    
Post your code so we can take a look at it.
_Amy 31-Aug-12 3:30am    
What have you tried yet? Where is your code?
[no name] 31-Aug-12 3:33am    
protected void gvStock_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label ddlBookId = (Label)e.Row.FindControl("lblId");
DropDownList ddlBookName = (DropDownList)e.Row.FindControl("ddlBookName");
Exception ex;
DataTable dtblBookName = new DataTable();
BllBookMaster obj = new BllBookMaster();
switch (obj.GetAllBookNames(ref dtblBookName, out ex))
{
case 1:
DataRow dr = dtblBookName.NewRow();
dr["bookid"] = "-1";
dr["bookname"] = "Select";
dtblBookName.Rows.InsertAt(dr, 0);
ddlBookName.DataValueField = "bookid";
ddlBookName.DataTextField = "bookname";
ddlBookName.DataSource = dtblBookName;
ddlBookName.DataBind();

break;
case -1:
lblMsg.Text = ex.Message.ToString();
break;
case 0:

lblMsg.Text = "error";
break;

}

}
}
catch (Exception ex)
{
lblMsg.Text = ex.Message.ToString();
}
}

While binding dropdown list control you need to set three properties.
Say "ddlBook" is dropdown control. It should be as shown below.

C#
ddlBook.DataSource = dictCountries;
ddlBook.DataTextField = "Text";
ddlBook.DataValueField = "Value";
ddlBook.DataBind();


If you set the text and value fields. You will be able to access the value with below code:
C#
string selectedVal = ddlBook.SelectedValue.ToString();


If you are able to find the instance of the drop down inside the grid row, then the above code works.
 
Share this answer
 
Comments
[no name] 31-Aug-12 3:43am    
but this selectedVal not accessible there on the save click
Dasaradhi_r 31-Aug-12 3:48am    
What do you mean by not accessible? Is it having null value?
Dasaradhi_r 31-Aug-12 3:48am    
And also tell me this:
On Save click, are you able to get the instance of the drop down inside the GRID ROW?
[no name] 31-Aug-12 3:55am    
can you please tell me how to find instance of dropdown list
Dasaradhi_r 31-Aug-12 4:07am    
Say "ddlBook" is the dropdown control, "gv" is your grid view.
If you want to find control from 1st row of the grid view, It should be done like below:

DropDownList ddlBookInstance=(DropDownList)(gv.Rows[0].FindControl["ddlBook"]);
string selectedVal = ddlBookInstance.SelectedValue.ToString();

if you want to loop through the rows, you can do it like below

foreach (GridViewRow row in gv.Rows)
{
DropDownList ddlBookInstance = (DropDownList)(row.FindControl["ddlBook"]);
string selectedVal = ddlBookInstance.SelectedValue.ToString();
}
Say "ddlBook" is the dropdown control, "gv" is your grid view. If you want to find control from 1st row of the grid view, It should be done like below:
C#
DropDownList ddlBookInstance=(DropDownList)(gv.Rows[0].FindControl);
string selectedVal = ddlBookInstance.SelectedValue.ToString(); }

if you want to loop through all the rows, you can do it like below:
C#
foreach (GridViewRow row in gv.Rows) 
{
 DropDownList ddlBookInstance = (DropDownList)(row.FindControl); 
 string selectedVal = ddlBookInstance.SelectedValue.ToString(); 
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900