ASP.NET GridView Can't hide Columns - c#

So I have an <asp:Gridview> and in my C# file, I am setting the datasource to some database table, and doing .DataBind().
However, I want to hide a column in the table based on a Boolean variable.
Something like this:
gridview.Columns['Field5'].Visible = false;
Or perhaps:
int c = gridview.Rows.Count();
for(int i = 0; i < c; i++){
gridview.Rows['Field5'].Remove();
}
Perhaps I cannot make it invisible, but I'm sure I can at least loop through and remove all rows related to the column "field5". I don't know how to go about doing this.
Does anyone perhaps have a proper link to using the GridView Class and how all the methods are suppose to be used because it's not clear, perhaps not written by microsoft?
Sorry if this is simplistic, the internet seems to lack a lot of C# documentation (or maybe it's just cluttered with too much useless ASP.net information).

No problem, the trick is that you have to reference the column by its index, not its name.
grid.Columns[1].Visible = false;

In your case you should loop through gridview and set cells you want to hide Visible=false
foreach (GridViewRow gvr in gv.Rows)
{
//here specify cell you want to hide
//also you may put any conditions
gvr.Cells[0].Visible = false; // Hide cell
}
To hide column you should go like that:
//here specify column you want to hide
grv.Columns[0].Visible = false; // Hide column
Also check this article http://www.codeproject.com/KB/webforms/Datagrid_Col_Example.aspx

I found a good method for doing something like that.
((DataControlField)gridView.Columns
.Cast<DataControlField>()
.Where(fld => (fld.HeaderText == "Title"))
.SingleOrDefault()).Visible = false;
I found it in below link
GridView Hide Column by code

The gridview is fine if you just want to quickly display some data, but don't need to modify it in any way. However if you do want to customize it, it is unpractical. It is easier and faster to build the table dynamically in your codebehind and you get all of the control you need.
Check out the first answer to this post: How to show pop up menu from database in gridview on each gridview row items?

Related

How to put a visibility on all rows of a DataGridViewRow in C#?

I am using the following code.
foreach (DataGridViewRow row in DataGridView1.Rows)
{
row.Visible = true;
}
However delayed, I wanted to know if you have any properties faster for this.
You just need to call datagridview.Invalidate() to have the draw happen.
There doesn't seem to be a neat solution to your problem. There is no property of the DataGridView that will make all rows visible/invisible with one line. An idea would be to set the data source to null to hide the rows, then when you want to show it again, you can set the data source back to the original. This is somewhat of a hack and not clean solution.
Alternatively, if it suits your needs, you can show/hide your entire DataGridView by setting the Visible property to true/false respectively.

Asp.Net C# Dynamic GridView Options and Values on Sorting

I have a GridView Control that displays data from a table on Page_Load as well as a progress bar in one column and a button in another.
It then loops through each row of the Table and depending on the values, hides said button (i.e. if the 1st column value = "Open" then the button in the 8th column is hidden). It uses a simple foreach loop;
foreach (GridViewRow Row in MyGridView.Rows)
{
if(Row.Cells[0].Text == "Open")
{
Row.Cells[7].Text = "";
}
}
This is working absolutely fine on page load... unfortunately once the user sorts the data by the column values it doesn't work. I can get the event to fire (testing between OnSort and OnSorting amongst other events on the board) but it isn't actually making any changes to the table.
It seems the issues lays somewhere in how I'm attempting to initiate it... does anyone have any ideas?
Thanks in advance.
Managed to work this out through more testing. For anyone with a similar problem, use the DataGridViews OnPreRender Event.

Coded UI Test - Click Control Based On Other Things On Table Row

im making a Coded UI test in Visual Studio 2010 for a web application in C# and i wish to click a button in the 6th column of a table based on the inner text of column 1? is this possible?
So for instance i have a table with Names in column one and other info and then a button in column 6. All auto generated.
Im assuming if i can get the Row number from the cell with say "John Smith" in it i can then press the button for this row in column 6.
Any ideas? i have tried googling and looking at what parameters i can pass in but im coming up stumped.
Something based on the following may help.
Access the HTML table by copying code from that generated by the Coded UI recorder for an assertion or a click on the cells. You should have something like:
HtmlCell theTable = new HtmlCell(this.UItheWindow.UItheTable.UIItemTable);
Cells of the table can be accessed by adding properties such as:
theTable.FilterProperties[HtmlCell.PropertyNames.RowIndex] = "0";
theTable.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "6";
UITestControl cell = theTable.Find();
HtmlCell wantedCell = (HtmlCell)cell;
The above might be used as the body of a method returning the value of wantedCell. The name should now be available as:
wantedCell.InnerText;
Accessing the button to be clicked should follow a similar approach.
Another approach uses the GetChildren method to traverse the table. Start by getting theTable as above. Then have something like
UITestControlCollection children = theTable.GetChildren();
Loop through the children checking row properties. When the required row is found, call the GetChildren of that row and get the loop through them to find the required column. Some points: You may need to loop on columns before looping over rows. You may be able to directly index into the UITestControlCollection for the rows and the columns rather than needing to loop and check values. Depending on exactly how the table was written there may be additional levels between the table and the wanted cells, so you may need to examine children, grand children, great grand children, great great ..., and so on.
I have a couple of extension methods that I use to tackle content in tables(Hand coding, rather than using the recorder) -
This extension method for a table gets the first row in the table that contains the requested text in one of its cells or controls
public static HtmlRow GetRow(this HtmlTable table, string cellContent)
{
if((UITestControl)table == (UITestControl)null)
throw new ArgumentNullException("table");
if(cellContent.Length > 80)
cellContent = cellContent.Substring(0, 80); //Our table cells only display the first 80 chars
//Locate the first control in the table that has the inner text that I'm looking for
HtmlControl searchControl = new HtmlControl(table);
searchControl.SearchProperties.Add(PropertyNames.InnerText, cellContent);
//Did we find a control with that inner text?
if(!searchControl.TryFind())
{
//If not, the control might be an HtmlEdit with the text
HtmlEdit firstTableEdit = new HtmlEdit(table);
//Get all of the HtmlEdits in the table
UITestControlCollection matchingEdits = firstTableEdit.FindMatchingControls();
//Iterate through them, see if any have the correct text
foreach (UITestControl edit in matchingEdits)
{
if(cellContent == ((HtmlEdit)edit).Text)
searchControl = (HtmlControl)edit;
}
}
//We have(hopefully) found the control in the table with the text we're searching for
//Now, we spiral up through its parents until we get to an HtmlRow
while (!(searchControl is HtmlRow))
{
searchControl = (HtmlControl)searchControl.GetParent();
}
//The control we're left with should be an HtmlRow, and should be an Ancestor of a control that has the correct text
return (HtmlRow)searchControl;
}
Once you're able to get the correct row, it becomes relatively easy to get the correct control in that row(Or the correct control in a given cell in that row)
In your example, you've got a button in the 6th column of the row. The button probably has some properties associated with it, but even without them if we can correctly limit our search it will still work. Assume our table is named UITableCustomers - Declare a new button and limit it to only the 6th(Index 5) cell in the row containing "John Smith"
Mouse.Click(new HtmlInputButton(UITableCustomers.GetRow("John Smith").Cells[5]));
Obviously, this call is going to fail if the given text doesn't exist in a control in the table.
Your question is not very clear to me but check out the documentation on jQuery.trigger
This method will let you emulate a clickevent. I hope this is what you need.

TableAdapter selected GridView Row and insert?

I am trying to get something of a selected GridView Row which is controlled with TableAdapter. (Visual Studio 2010, ASP.NET)
I have otobas.xsd. Inside that, I have Otobus and Sefer tables and also SeferTableAdapter and OtobusTableAdapter. I fill the gridview like this;
otobasTableAdapters.SeferTableAdapter sef = new otobasTableAdapters.SeferTableAdapter();
otobas.SeferDataTable sefs;
sefs = sef.GetData();
SeferTableAdapter sefers = new SeferTableAdapter();
GridView1.DataSource = sefers.GetData();
GridView1.DataBind();
On the GridView Table, I have Select buttons on each row, therefore, when the user clicks one of the select buttons, I want to know which row it was. And then, I want to get the row like this (however, I can't)
otobas.SeferRow rw = ...?
I don't know how to do that. I tried many things but I couldn't. After I get this rw, I want to do something like;
string id = rw['id'];
string arrival = rw['arrival'];
However, I just couldn't do it. Can you show me a way to do this? I tried to add a row but I also couldn't do it. I don't know how to create a row I think. I tried to do the same as
http://msdn.microsoft.com/en-US/library/5ycd1034%28v=VS.80%29.aspx
but, when I wrote
otobas.SeferRow newRow = otobas... I have a data table named Sefer, but I can't reference it at all. Is it wrong to make an instance of the otobas dataset? Like
otobas oto = new otobas();
otobas.SeferRow sr = oto.Sefer.... ?
Why do I have to create an instance of otobas? I mean I already have it as otobas.xsd I just don't get it.
Haven't used this before but is it possible to implement an event handler for the onlclick event of this grid (in either client side or server side code) where you can make use of the event arguments to find this an other information you are searching for>?

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