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
Related
I'm querying a database filling a gridview with values, also adding dropdown boxes into each cell within the dataview 'onrowdatabound' so these DDL's are populated when the gridview is populated.
I want to be able to click a button to get values from these DDL's however when the button is clicked postback happens and all the DDL's disappear and it gives me the default value for the DDL.
I assume they dissapear as they're not called on pageload (which I can't seem to do as they're called onrowdatabound)
<asp:GridView id="View" name="Spview" onrowdatabound="populateCellswithDDls" runat="server"></asp:GridView>
Adding the ddl with inside 'populateCellswithDDls' function looping each cell:
e.Row.Cells[i].Controls.Add(DDL1);
The next thing I've have a play with is ViewState and Sessions to save the dropdownlists on postback(Tried making sessions within 'populateCellswithDDls' function as so:
DropDownList DDL1 = new DropDownList();
//I've tried newSkillsMon.AutoPostBack = true; but this just removes them all too
Session.Add("ViewState", View);
Session.Add("DropdownState", DDL1);
I've tried all sorts do to with viewstate and session but unsure where to use them in relation to saving states the 'onrowdatabound' population.
My button currently looks like this:
protected void confirm_Click(object sender, EventArgs e){
{foreach (GridViewRow row in View.Rows)
// if (DDL1.SelectedItem.Value != "Select Item"){
if (IsPostBack)
{
Debug.WriteLine(DDL1.SelectedValue);
}
This just gives me X amount of "Select Item" rather than what i have selected in the DDL
What am I missing, where would I add these sessions to keep the ddl's created by onrowdatabound?
Thanks
When creating dynamic controls, you need to recreate them on every Page Load and that includes a PostBack. So start with binding the GridView data outside an IsPostBack check.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//normally you would bind here
}
//but now bind grid every page load
GridView1.DataSource = Common.LoadFromDB();
GridView1.DataBind();
}
Now in the RowDataBound event make sure the DropDownList has an ID
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//create a dropdownlist and assign an ID
DropDownList DDL1 = new DropDownList();
DDL1.ID = "DDL1";
//add some dummy listitems
DDL1.Items.Insert(0, new ListItem() { Text = "A", Value = "A" });
DDL1.Items.Insert(1, new ListItem() { Text = "B", Value = "B" });
DDL1.Items.Insert(2, new ListItem() { Text = "C", Value = "C" });
//add the control to the row
e.Row.Cells[0].Controls.Add(DDL1);
}
}
Now you can get the value from the correct row on a button click.
protected void Button1_Click(object sender, EventArgs e)
{
//find the dropdownlist in the correct row and cast it back to one
DropDownList DDL1 = GridView1.Rows[i].FindControl("DDL1") as DropDownList;
//display the result
Label1.Text = DDL1.SelectedValue;
}
Something that might work for you instead of trying sessions is to use hiddenfields.
Make a hidden field for each dropdown location and then use javascript to populate the hidden fields with the dropdown values. (Possibly wanting some server side validation on submission)
Something like this with jQuery:
$(".dropdowns").on("change", function () {
$(this).closest('input:hidden').val($(this).val());
});
And in your confirmation:
if (HF_HiddenField1.Value != "Select Item")
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";
}
}
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;
}
}
}
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
how to skip specific item in row bind event C#
You could
Filter the data via the SQL query.
Filter the data via a table select if binding to a datatable/dataset
Filter data via a DataView
Use the RowDataBound event
Edit ~ Here is how to use the RowDataBound
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow) {
//A value to check for
string myValue = DataBinder.Eval(e.Row.DataItem, "myColumn").ToString();
if ((myValue == "a")) {
//Hide the row
row.Visible = false;
}
}
}
I am not sure how this will affect your paging....if you do have one.