The best way to conceal DataKeyName column - c#

There are many ways to hide a column but how do you hide one in GridView and retaic access to values in that column?

After databinding set the Column's Visible property to false.

The best way includes 3 steps.
1. Assign DataKeyName in Grid definition.
2. Set Visible to false in column definition - that will prevent sending values to the browser.
3. Use SelectedIndexChanging event of Grid view. As this event is fired long begore PageUnload the data in column still accessible. You can store that value in ViewState, Session or just pass it to another method.
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
if (e.NewSelectedIndex != -1)
{
ViewState.Add("myKeyValue", GridView1.DataKeys[e.NewSelectedIndex].Value);
}

Related

check if datagrid has been sorted

In the datagrid Item Databound event, I want to know if the grid has been sorted or loading for the first time. I know there is an event OnSortCommand and I can set some variable here to check if there is a sort happening on the grid. But I want to know if there is a better way to check if the grid has been sorted. Thanks in adv. for your help.
PS: I took a look at this post and it suggests to check for Request.Form["__EVENTTARGET"] and Request.Form["__EVENTARGUMENT"]. Let's say I have 'x' number of columns in the grid and other server controls on the form, I feel it's not a correct way to have 'x' If conditions and check if the request is from one of those many controls.
If a DataGridView is sorted, its SortedColumn property will be set.
Here is an example to verify the sort order of the columns of the DataGridView control or checking the status of the property sortorder.
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Sorted +=new EventHandler(dataGridView1_Sorted);
}
void dataGridView1_Sorted(object sender, EventArgs e)
{
if (this.dataGridView1.SortOrder.Equals(SortOrder.Ascending))
{
// your code here
}
if (this.dataGridView1.SortOrder.Equals(SortOrder.Descending))
{
// your code here
}
if (this.dataGridView1.SortOrder.Equals(SortOrder.None))
{
// your code here
}
}
Regards
The suggestion from the post you linked is rather inelegant ;)
I suggest you read this, which as very straight forward example. It uses a DataView to sort the data. If you want to "remember" for whatever reason, the last sort expression that was used to sort the grid, you can simply store it in ViewState as so:
ViewState["LastSortExpression"]=e.SortExpression;
And retrieve it on PostBack as needed.

Finding the click event of DGV button column and transferring to another form

I have a datagrid view with four columns:
productid
productname
productprice
buy (this is button column )
Is it possible to find the click event of button column? I mean, if I click on the row 1 button, the corresponding row values will be transferred to another form.
If I click on the row 2 button, the corresponding values are transferred to another form. I am doing WinForms applications. Any ideas or sample code would be appreciated.
This is answered on the MSDN page for the DataGridViewButtonColumn.
You need to handle either the CellClick or CellContentClick events of the DataGridView.
To attach the handler:
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
And the code in the method that handles the evend
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Check that the button column was clicked
if (dataGridView1.Columns[e.ColumnIndex].Name == "MyButtonColumn")
{
// Here you call your method that deals with the row values
// you can use e.RowIndex to find the row
// I also use the row's databounditem property to get the bound
// object from the DataGridView's datasource - this only works with
// a datasource for the control but 99% of the time you should use
// a datasource with this control
object item = dataGridView1.Rows[e.RowIndex].DataBoundItem;
// I'm also just leaving item as type object but since you control the form
// you can usually safely cast to a specific object here.
YourMethod(item);
}
}
using validate cell you can get the (column & row of the cell). Initialize the button as win forms button object, also add a handler that call the click event of the same.

Hiding/disabling ASP.NET controls when SqlDataSource is empty

I need to be able to hide or disable a multitude of items (detailsviews, gridviews) when an SqlDataSource returns no rows. So if the page is reposted and no rows are selected, all the controls would be disabled.
How would I do this?
Thanks
You can try to hook to the DataBound event of GridView. If there are no rows, set Visible=false to all the controls you need to hide. Example:
<asp:gridView id="control" OnDataBound="DataBound" [...]
protected void DataBound(object sender, EventArgs e)
{
control.Visible = control.Rows.Count>0;
}
I normally use LINQ and wrap my results in a Collection or Dictionary, but as best as I can tell, you can do this by adding a new handler to the .Selected event (provided that your DataSourceMode is SqlDataSourceMode.DataSet. If you check the AffectedRows property of the SqlDataSourceStatusEventArgs in that event, it should tell you how many rows were returned (at least, the documentation implies as much).
Once you know that, you can proceed to disable or enable your other page controls.

Change the value of every cell in a DataTable column

I have a System.Data.DataTable which I'm binding to a GridView. If the user doesn't have a certain security role, I want to replace the data in certain columns with an "X". Can I do this without looping through all of the rows?
Implement the GridView's RowDataBound event and make the change in there if the user's security role is inadequate.
void CusomGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(User.Role != "Admin")
{
e.Row.Cells[1].Text = "X";
}
}
Strictly speaking, no. You can use Linq-style extension methods to hide the implementation, but any code in a procedural language is going to involve an iterative loop through the rows. Even the RowDataBound event is fired iteratively by each row; however, it's iterating through the rows anyway, so at least you're not duplicating the looping behavior.
You could also do it on the back end. Pass the userID or role to an sp which returns only viewable columns and 'X' values for columns they can't see and bind the UI to that.
You could simply have a second column available and ready name it "your column2"
filled with x data, you then determine which column is visible based on admin access level.
In VB.NET you cannot change values of each row without recursion.

Gridview making a column invisable

Hi I have a grid view which is dynamically generated via C# using a DataSet.
I'm passing a row ID field (from the database) to the grid view, as this is required when a user clicks to edit a records. However I don't want the user to be able to view the column.
I have tried the following but it doesn’t seem to work? Could anyone suggest a way to hide the column or a better way of attaching the information to the grid view row so it can be used at a later stage.
c#
DataColumn ColImplantCustomerDetailsID = new DataColumn();
ColImplantCustomerDetailsID.ColumnName = "Implant ID";
ColImplantCustomerDetailsID.DataType = typeof(int);
ColImplantCustomerDetailsID.Visable = false; <-- visable doens't work here.
asp.net
DataColumn doesn't have a 'Visable' property. Heck, it doesn't have a 'Visible' property either.
Use this before you bind
gridviewNameHere.Columns[index].Visible = false;
You can make it visible again on one of your event handlers.
Another option, rather than hiding the column, is to use the DataKeyNames property of the GridView to store the name of the ID field. You can then use myGridView.SelectedValue to retrieve the selected ID.
Use the index of the column to hide it when you initialize it...
grid.Columns[index].Visible = false;
I actually hide a few things as you are suggesting, example - database status names for that row that can be used in code that the user does not need to see.
Accessing GridView Invisible Columns
http://www.highoncoding.com/Articles/178_Access_GridView_Invisible_Columns.aspx
In the OnRowDataBound event of the GridView component:
// c#
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
int index = 0; // put the index of the column you need hide.
e.Row.Cells[index].Visible = false;
}

Categories

Resources