Using C# & asp.net
if there is no data's in the table, gridview displaying one rows (it was empty)
I want to hide the gridview empty row.
How to do this.
Assuming that you can normally delete that one visible row just check that if a field that would normally have a value is empty and the row count is 1 then delete that row.
if(String.IsNullOrEmpty(mydatagrid.Rows[0][0].ToString()) && mydatagrid.Rows.Count==1) //Check a field that would normally have a value
{
mydatagrid.Rows.RemoveAt(0);
}
Let me know if this helps/works
If you are manually data binding than you can check at that time and hide or disable the control if there is no data. If you are data binding using the design view than you should subscribe to the DataBinding or the PreRender events on the control and check then.
you can check if the datatable doesn't have any rows
use:
mydatagrid.DataSource=null;
mydatagrid.DataBind();
As the other two comments you can either check in code and set MyDataGrid.Visible to true or false to hide the entire table, or you can not bind the datasource, or you can EmptyDataTemplate option to display whatever you want when there is no data for the GridView to display normally.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
The normal behavior for GridView is to render NOTHING if there are no data rows. (Well, almost nothing; it insists on rendering a <div> wrapper, but that'll be empty.)
If you specify an EmptyDataTemplate, however, a one-celled wrapper table is generated to contain whatever is in your template. Even if your template is empty, you'll get that wrapper table (with its one cell, empty). Sounds like the answer to your question is don't specify an EmptyDataTemplate.
Related
I use a DataGridView to view and edit data in my PostgreSQL database, which works fine. I want to make it a little more user-friendly by choosing the right Control to input data. Specifically, I want to create a ComboBox in a column to let the user search and select a value fast.
To do so, I think I need the EditingControlShowing event to fill the combobox. However, the column I get is of type DataGridViewTextBoxColumn, so the corresponding e.Control is a TextBox instead of a ComboBox.
I never initialise those columns, because they come from views in the database. How can I cast/initialise the column to DataGridViewComboBoxColumn ?
This is how I populate my DataGridView:
dgView.DataSource = getView();
getView() returns a DataTable as can be gotten from NpgsqlDataAdaper.
Since you want to use something other than a DataGridViewTextBoxColumn you need to set AutoGenerateColumns to false and actually define the columns yourself. When defining them, choose a DataGridViewComboBox column for the appropriate fields.
This can be done in code, or in the designer, and based off your question I think you should be able to simply do it through the designer.
You should do it in 2 steps.
1. First of all ( as mentioned by Michael) you should set AutoGenerateColumns to false and then create the required columns using the datagrid view columns option (in properties)
2. Then you should loop through your records and assign appropriate values to appropriate columns.
In case you have hundreds of records that you are showing in grid view, than you should consider using server side paging.
Is there any way to refreshing a cell of a grid view without refreshing the entire grid view in c# asp.net
you could possibly use a templatefield, put an UpdatePanel in it and refresh that single cell that way. You would just need someway to initiate the refresh.
You may have to use a nested UpdatePanel within the cell.
If you don't want to put an UpdatePanel in every cell in your GridView, you can refresh a cell on a regular, full postback. The contents of the grid will be roundtripped, but your data source won't necessarily be queried in full.
First, make sure your grid doesn't rebind on every postback, e.g. wrap your gv.DataSource = x; gv.DataBind(); inside an if (!this.IsPostBack).
Then you should be able to do something along the lines of
((Label)gv.Rows[x].Cells[y].FindControl('myLabel')).Text =
GetDataItemNumber(x).FieldForColumnY.ToString();
Bear in mind that gv.Rows[x] may not correspond to the xth item in your data source, as Rows includes header rows etc. You might need to iterate through Rows checking e.g. IDs to find the correct row.
Disclaimer: I've never actually done this before, but it should be possible...
Similar questions may exists, but none of them seems to be helpfull. So I'll try to explain a more specific case, and see if anyone can help me :)
Here is the thing, I have a gridview with templatefields, and I want to let the user to specify the order those columns are shown. So, the user creates a view and decides which column to display first, second, and so on.
So basically, I need to change the order of the columns just after the grid is loaded with data.
Sounds easy huh? Well, apparently it's not. At least I couldn't achieve that just yet.
Some notes:
- Of course I have AutogenerateColumns set to false.
- Change the sql select columns order won't work because of previous item.
- And I'd like not to generate the columns by the code.
Any ideas?
You can modify the Gridview's Columns collection in your code-behind. So, one way of doing this is to remove the column from its current position in the collection and then re-insert it into the new position.
For example, if you wanted to move the second column to be the first column you could do:
var columnToMove = myGridView.Columns[1];
myGridView.Columns.RemoveAt(1);
myGridView.Columns.Insert(0, columnToMove);
If you need to move them all around randomly, then you might want to try to clone the field collection, clear the collection in the GridView, and then re-insert them all in the order you want them to be in.
var columns = myGridView.Columns.CloneFields();
myGridView.Columns.Clear();
myGridView.Columns.Add(columns[2]);
myGridView.Columns.Add(columns[0]);
etc..
I'm not 100% sure whether this all will work AFTER binding to data, so unless there's a reason not to, I'd do it in Page_Init or somewhere before binding.
I know this is an really old question but the suggested answer did not solve the problem fully.
The ViewState for the GridView will failed after remove / add a dynamic column.
If you need to bind any event, like OnRowCommand and OnRowUpdating, such events will not be fired.
Edit:
Look into this function if you need answers.
Protected Overrides Function CreateColumns(dataSource As PagedDataSource, useDataSource As Boolean) As ICollection
I have a program that I use to enter values into a database using a gridview. The gridview is populated with an 'empty' result from a DataSet that queries that database from the table I want. This fills the grid with the proper columns that I want (ie. a grid with an empty row from a certain table in a db). When you fill in the columns, I take the data and manually update the db.
Basic example of how I use the gridview:
this.fullUutDataTableAdapter.Fill(this.dalsaUutDataSet.ResultsFullUut);
DataView dv = this.dalsaUutDataSet.ResultsFullUut.DefaultView;
Grid_modify.DataSource = dv;
foreach(DataGridViewRow dr in Grid_modify.Rows)
{
dr.Cells["YieldID"].Value = -1;
}
However, I want to have certain columns to have Default Values. I have tried changing the column's default value, but you cannot change the property, only 'GET' it. Alternatively, I tried parsing through the rows in the empty grid and filling in the values. This works fine when the grid loads up initially (values show up in proper places). HOWEVER, when as soon as I enter edit mode these values clear to nothing.
NEXT, I tried using some of the RowEdit Events and tried filling in values when edit mode is entered, however, this causes problems when you enter and leave edit mode. Seems to be a more complicated and messy way of doing things that seemingly necessary.
So the question really is, what is the best way to fill a gridview with default values in certain columns?
I would prefer the "cleanest" solution possible. However, I recognize that things don't always work out that way.....
Thanks for your help in advance! Cheers.
Take a look at this: How To Specify Default Values for DataGridView
I'm writting a simple prototype front end using a GridView that is populated via function, rather than being linked directly to a SqlServer data source.
So I can delete a row/record from grid/underlying database I am currently
Setting the AutoGenerateDeleteButton = true
Displaying the unique record ids in the first column
Handling the RowDeleting event
Obtaining the id by getting the grid.Rows[e.RowIndex].Cells[idIndex].Text
Passing that number through to a function that does the deleting
This seems to be working just fine, but I would rather not display the ids to the users at they don't mean anything to them.
I tried setting the id column's Visible property to false, but this caused step 4 above to return an empty string - and so no record deleted.
So how do I store the hidded id number with each row?
Or am I going about this completely the wrong way?
Follow up to answers:
Thanks for both the answers, ended up going Eric's DataKeyNames way. For other people new to ASP.NET like I am, the steps I used where
Between the lines where I set the grids DataSource and called DataBind(), I added
grid.DataKeyNames = new string[] {"id"};
Then in the function handling the RowDeleting I got hold of my id using
grid.DataKeys[e.RowIndex].Value
GridView has a DataKeyNames property. When you bind a data source to the grid, you set the DataKeyNames (usually with just one name, your PK field). You don't show the PK, but you can get to it from code-behind.
Visible=false means don't render on the page. What you want is either to make it a template field and use a HiddenField to hold the value or set the style on the control to "display: none;". This would be the case if the client side code needed access to the value for an Ajax call or something.
Otherwise use the DataKeyNames property as #Eric Z Beard suggests.