combobox column gridview - asp.net

I need to aske I have gridview with combobox template column and its fill with items .
when select index changed i need to set description value in onther cell but I couldn't get the index of the gridview row?
any help

It's a little unclear which SelectedIndexChanges you are talking about, but I assume you think about the combobox SelectedIndexChanged.
Assuming you have hooked up the eventhandler on the combobox, you can use the following code to get the RowIndex inside the handler
protected void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
int idx = (((sender as System.Web.UI.WebControls.DropDownList).Parent.Parent as GridViewRow)).RowIndex;
}
Parent of the dropdownlist will be a DataControlField and the parent if it will be the GridRow.
This also assumes you have no other container controls inside your templatefield, as the parent.parent structure then could be different.

Related

How to get values from Controls which are in GridView?

I am new to asp.net website developer.
In my website I use GridView Control.In the GridView row i place some controls like
TextBox,DropDownList.
I can display the values in GridView.
But my requirement is ,Get the values from TextBox and DropdownList Which are existed in GridView.
Please help me to go forward..
thank you,
bye..
You need to access them by row. This code project article explains it in detail.
TextBox tb = (TextBox)gridview1.Rows[0].FindControl("idOfTextBox");
It above statement will find control in the first row of grid which has id idOfTextBox and type is textbox.
You can directly get any value of any control by casting directly, without using an extra textbox or dropdown variable.
Example:
string val = ((TextBox)gridview1.Rows[0].FindControl("TextBox_ID")).Text;
Hope it helps :)
Subscribe the RowDataBound event(this will be fired on each row available in gridview) of gridview and access like stated below,
protected void grdBillingdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//these controls are available inside gridview
HiddenField hdnNagetiveAmt = (HiddenField)e.Row.FindControl("hdnNagetiveAmt");
DropDownList ddlFeeType = (DropDownList)e.Row.FindControl("ddlFeeType");
TextBox txtFeeDesc = (TextBox)e.Row.FindControl("txtFeeDesc");
Button btnUpdate = (Button)e.Row.FindControl("btnUpdate");
}
}
not only text box we can get values from all the controls that used inside gridview that placed by using itemTemplate or simply a Bound field .
you need to loop for each row to get the values
foreach (GridViewRow row in grd_popup_details.Rows)
{
//get control values
}
Refference link

use button click event to load usercontrol

Now I am trying to use a button on the parent page to a usercontrol.
There is a checkboxlist on the usercontrol, and its datasource is read from database
the checkboxlist didn't load correctly. it is like:
OnlineRenewa_draft1.tbl_LanguageChoices OnlineRenewa_draft1.tbl_LanguageChoices OnlineRenewa_draft1.tbl_LanguageChoices
OnlineRenewa_draft1.tbl_LanguageChoices OnlineRenewa_draft1.tbl_LanguageChoices OnlineRenewa_draft1.tbl_LanguageChoices
is it about postback issue?
Any idea?
thanks
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack) {
List<tbl_LanguageChoices> LanguageList = ((List<tbl_LanguageChoices>)Cache["LanguageChoise"]);
otherlanguage.DataSource = LanguageList;
otherlanguage.DataBind();
otherlanguage.DataTextField = "Languages";
otherlanguage.DataValueField = "GUID";
}
You're first databinding the checkboxlist's to the DataSource and then setting DateValueField and DataValueField. DataBind should be the last step.
otherlanguage.DataSource = LanguageList;
otherlanguage.DataTextField = "Languages";
otherlanguage.DataValueField = "GUID";
otherlanguage.DataBind();
Have you set the DisplayMember property of your CheckBoxList? You need to set it to the column/property of the object you have set as the datasource. Or as MSDN puts it:
Gets or sets a string that specifies a property of the objects
contained in the list box whose contents you want to display.

GridView column width

I'm using VS 2010. I have a GridView with few template columns. I want the 2nd column to not be visible at all, but still to be existed so javascript will be able to see it's value.
Does someone knows how to set this width value?
Thanks
Place a HiddenField in the first column and put the value that you need to put it in the second column in it instead of creating the second column.
The Problem:
Your problem originates from the fact that when you hide a data-bound GridView's column, its bounded value is no longer available and if you tried to access it you will get an empty string.
The solution:
Enable 2 events in in your gridview:
RowDataBound: In this event you can access the hidden cell value (before hiding it yet)
protected void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs)
{
// Here you store the value
this.sID = e.Row.Cells[1].Text;
}
RowCreated: In this event you hide the cell, write this in the event handler:
protected void MyGridView_RowCreated(Object sender, GridViewRowEventArgs)
{
// then you hide the cell (Only the cell not the column)
e.Row.Cells[1].Visible = false;
}
In these codes, after we save the value we need in another variable/array whatever, we can easily hide the cell. You can put that value in a hidden input to enable accessing the value from javascript.

When GRidView's row is in edit mode, textboxes don't display current values

1) I noticed that if we don’t bind GridView to object data source control, then when user puts GridView into edit mode, we have to handle GridView.RowEditing event (else we get an exception ) and in this event put GridView’s row into editing mode. Is there a reason why GridView doesn’t automatically put a row into edit mode?
2) When we manually bind GridView to one of DataSet’s tables and user puts a row into edit mode, row’s columns will replace fields with text boxes. But for some reason these text boxes don’t display current field values, but instead they don’t display any text at all. What am I doing wrong?
3) I’ve also handled gridView.RowUpdated event, so I could put row back into non-edit mode, but to no effect. I even tried by pressing Edit button of some other row, but row still wouldn’t go out of edit mode. Any ideas what I’m doing wrong?
protected void gvwEmployees_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
e.KeepInEditMode = false;
}
Thanx
When not using a DataSource control with a GridView or other data-bound control which hide the complexity of the manual data-binding you must manually handle RowEditing, RowUpdating, and RowDeleting etc. With the built in data model and automatic binding the GridView handles these events for you.
You haven't posted your RowEditing code, but i suspect that you are not setting the GridViews EditIndex to the NewEditIndex and are not rebinding, this is probably why you are not seeing current data.
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView.EditIndex = e.NewEditINdex;
BindData();
}
The same is true for your RowUpdating event. You will have to manually update your data, then set the EditIndex to -1, this will put your GridView back into ReadOnly mode. Keep in mind that e.OldValues, e.NewValues and e.Keys properties of the GridViewUpdateEventArgs are not populated when binding manually. This mean you'll have to take care of the update yourself by using e.RowIndex which is the index of the edited row.
protected void gvwEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView.EditIndex = -1;
BindData();
}

Asp.Net Gridview Buttonfield get Row Data

I have a Gridview which is binded to a datatable that I created. The data in the table is ever changing.
I added a buttonfield to the Gridview. When clicked I want to send a specific columns value in that row through a link or sent through a function.
How do you go about doing this?
I found out how to do it. You set the commandName property to whatever you want to call it inside the properties.
Then if you double click on the button after creating it in design view, it should pop up a function in the code behind page. You can then access the row by doing the following.
protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowNum = int.Parse(e.CommandArgument.ToString());
}
from there you can use the row number to access specific column data.
Here's a decent example from MSDN: http://msdn.microsoft.com/en-us/library/bb907626.aspx

Resources