Trying to populate drop-down list in GridView edit - c#

I have the following code trying to populate drop-down list when I click edit on a Grid View, and it gives me the following error:
" 'ddlgvRoom' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value"
Any idea on why I need to add code into the row editing event, and if so can you help? My gridview is getting its values from an objectdatasource.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
DropDownList dl = (DropDownList)e.Row.FindControl("ddlgvRoom");
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList ddlgvRoom = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlgvRoom");
string strgvRoom = ddlgvRoom.SelectedItem.Text.ToString();
DropDownList ddlgvJack = (DropDownList)
GridView1.Rows[e.RowIndex].FindControl("ddlgvJack");
string strgvJack = ddlgvJack.SelectedItem.Text.ToString();
DropDownList ddlgvVlan = (DropDownList)
GridView1.Rows[e.RowIndex].FindControl("ddlgvVlan");
string strgvVlan = ddlgvVlan.SelectedItem.Text.ToString();
GridView1.DataBind();
}
}

Had to take off the selectedvalue off the HTML side and everything started working

Related

How to add Checkbox inside the Gridview dropdownlist

I need to do add Checkbox inside the Gridview dropdownlist, the dropdown list was added in RowDataBound event and it has fetch the data from database.
protected void grdupload_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
List<string> coll = new List<string>();
if (e.Row.RowType == DataControlRowType.DataRow)
{
string fnCriteria = ((DataRowView)e.Row.DataItem)["fnCriteria"].ToString();
DropDownList ddlfnCriteria = (e.Row.FindControl("ddlfnCriteria") as DropDownList);
coll.Add(fnCriteria);
ddlfnCriteria.DataSource = coll.ToList();
ddlfnCriteria.Text = fnCriteria;
ddlfnCriteria.DataBind();
}
}
Please suggest me to get a solution. Thanks in advance
You can follow these links.
1) http://www.dotnetgallery.com/kb/resource55-Checkbox-list-in-Dropdown-using-Aspnet-Ajax-PopupControlExtender-control.aspx
2) http://www.codeproject.com/Articles/66572/A-Multiple-Selection-DropDownList-a-CheckBoxList-I
3) http://www.dotnetspeaks.com/DisplayArticle.aspx?ID=63

How to replace text in some cells of GridView after data binding?

I have a GridView that is assosiated with database. Here's a data binding:
protected void GridViewProgramms_SelectedIndexChanged(object sender, EventArgs e)
{
int rowIndex = ((GridView) sender).SelectedIndex;
var programid = ((GridView) sender).Rows[rowIndex].Cells[1].Text;
GridViewEx.RowEditing += GridViewEx_RowEditing;
SqlDataSource1.SelectParameters["ID"].DefaultValue = programid;
GridViewEx.DataBind();
ExcersicePanel.Visible = true;
PanelAp.Visible = false;
}
Everything works fine, but I need to change some cells values in GridView after that. I need to rewrite every Cell in the last row. How to do this without affecting the database?
You will need to write your code(To change the cell text) in RowDataBound event of the gridview after data bind
Refer following link for more explanation
Update GridView Column after databinding
protected void GridViewEx_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
label or textbox ddltype = (label or textbox)e.Row.FindControl("id");
ddltype.text="ur text";
}
}

How Do I Bind Dropdown To Value Present In Database

I am using datalist control an asp.net using C# and sqlserver 2008.I have a dropdown in datalist and need to display it's current value from database on page load.
I have tried this so far,
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
DropDownList ddlshowit = (DropDownList)e.Item.FindControl("DropDownList4");
ddlshowit.DataSource = ds;
ddlshowit.DataTextField = "showit";
ddlshowit.DataValueField = "showit"; //showit is my column name
ddlshowit.DataBind();
}
}
I also tried adding the following declaration in markup: SelectedValue='<%#Eval("showit")%>' but it also didn't work. Please Help
You can find your dropdownlist from your datalist in this way,
Protected void Page_load(object sender,Eventargs e)
{
foreach(DataList dl in DataList1.Items)
{
DropDownList ddlshowit = (DropDownList)dl.FindControl("DropDownList4");
}
}
Let me know the output.

How to get GridViewRow from DataKeys

I know this type of question is asked before but no one got the answer yet...!!
How to get a Grid View Row from Data Keys.I don't want to iterate through the whole gird view.
I want to access specific text box(s) in a grid view.
for example in a 100 rows grid view i only want to disable any 2 text boxes on Page Load.
I have Data Key Names defined in grid, but how to get rows from it ?
any idea?
Please try following code..
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Get data row view
DataRowView drview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find textbox control
TextBox txtname = (TextBox)e.Row.FindControl("txtName");
string Name = txtname.Text;
if (((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString() == "Leave")
{
txtname.disable=true;
}
else
{
txtname.disable = false;
}
}
}

asp.net gridview set format for unbound field

i have a gridview populated by the code below:
protected void CautaProiect_Click(object sender, EventArgs e)
{
wipDBTableAdapters.GetSummaryProiectTableAdapter proiecte = new wipDBTableAdapters.GetSummaryProiectTableAdapter();
SummaryGrid.DataSource = proiecte.GetData(CodProiect.Text);
SummaryGrid.DataBind();
}
The gridview will be populated with some columns with values.
The problem is that the values are formated like this 1234.5600 and i want them to be like 1,234.56
How ca i do this ?
You can format your data in the OnRowDatabound event
sample:
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label l = (Label)e.Row.FindControl("lblValue");
l.Text = String.Format("{0:C}", l.Text);
}
}
In your GridView columns, use the DataFormatString property and set it to the format you prefer. In your case, you'll want to use "N2".
A great cheatsheet of other formatting options can be found here.
you can use the following code to display in the gridview ItemTemplate
<asp:Label ID="lblFinalPrice" runat="server" Text='<%#Convert.ToDouble(Eval("FinalPrice")).ToString("#.00")%>'></asp:Label>
I have finally managed to find an answer for this :
Here is how :
protected void SummaryGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
double value = Convert.ToDouble(e.Row.Cells[4].Text);
e.Row.Cells[4].Text = value.ToString("#,#.##");
}

Categories