GridView column width - asp.net

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.

Related

Click on Edit button in GridView puts wrong row in edit mode

I have gridview putting data from one table from database using stored procedure.
On click of Edit Button, post back is happen which refreshing the data on gridview and wrong row is shown in edir mode.
I have found the reason is that on click on EDIT link javascript:__doPostBack('GridView','Edit$0') is displayed at the status bar, which is a problem here. Edit$0 means the absolute value of the row and when postback get the data from data base wrong row is shown in edit mode...
I think the solution will be put the edit mode based on not row number but some unique value of the selected row for edit.
Please help if any one has answer to it.
If you're using a gridview I suggest using the native OnRowEditing="GridViewEditEventHandler"
and then in the code behind
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(v=vs.110).aspx
However, if for some reason you are creating an OnClick function when you press the edit button and then changing the Itemtemplate to show/hide values I would suggest doing
GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
from there you can do the .findcontrol to find the specific item you want ex:
TextBox txtbox = (TextBox)TaskGridView.Rows[RowIndex].FindControl("textBoxID");
Hope this helps

Hide Gridview Command Buttons

I have a gridview which has rows of 'tickets'. A user has the ability to 'claim' a ticket, this is all set up and works. What i need is to be able to change/hide the 'claim' button and replace it with a 'release' or 'open' command button. Can i achieve this at a rowdatabound level? What i do not want is to query the db everytime to see if the ticket is claimed.
You could cast the button from its Cell, then change its CommandName, and CommandArgs if needed, along with its text; eg use the same actual button for many purposes.
Im asssuming there is some status field that dictates what you can do with a record? Therefore on RowDataBound get this via a datakey, and adjust the button to suit.
Then on its click, check the command name and execute the relevant function / code block?
Edit - like this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Button btn = e.Row.Cells[YourButtonsColumIndex].FindControl("btnYourButtonsID") as Button;
btn.CommandName = "Release";
//Or
((Button)e.Row.Cells[YourButtonsColumIndex].FindControl("btnYourButtonsID")).CommandName = "Release";
}
Bearing in mind hidden rows still count in the zero-index column list.

combobox column gridview

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.

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