I'm working with a GridView that I'm populating programmatically. In order to access the ID value, I have to have it as one of the columns (as opposed to a ListBox, where I was able to assign the value to each entry in a hidden way). Is there a way to hide that ID column, but still use the value?
The columns are being auto-generated through the code and I'm accessing the ID value using DataKeys.
Here's a general overview of what you can do. It's not complicated. You databind as usual, but while doing that you find the index of the ID column. Then you use that index to hide the column on the RowCreated event. If you search for how to hide a column using auto-generated columns, you'll run into several answers following the method I'm using for hiding.
Private IDColumnIndex As Integer //Class scope - we need to use it in multiple methods
Public Sub PageLoad() Handles Me.Load //or wherever your databind is happening
Dim source As New DataTable()
IDColumnIndex = source.Columns("id").Ordinal //Gets column index of "ID" column
view.DataSource = source
view.DataBind()
End Sub
//Hide our "ID" auto-generated column.
Public Sub view_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles view.RowCreated
e.Row.Cells(IDColumnIndex).Visible = False
End Sub
Assign the ID to the DataKeyNames collection will work fine, is that an issue for you? The data keys is available for each row, not showing up in the UI, but still accessible with the row data, so you can get the ID for each row. You just have to get it from the DataKeys collection, instead of the row directly.
You can add code to hide the first column (the ID) so that it doesn't appear, and rely on using the datakeynames to get the ID value... if that doesn't work, could you elaborate on why so I can adjust my answer..
You can get the ID value if you add the ID column to the DataKeyNames property of your GridView. msdn link here
myGrid.DataKeys[myRow.RowIndex].Value.ToString();
I usually add a visible column But then make the template for the colmn jus a space or something. Then in the ID for the column, I put the ID I want to track. This technique let's me easily find the ID of a row using javascript when the content has been rendered into HTML.
If you let the grid view do its business rendering with the hidden data keys, you may never find the ID in client side browser javascript in all that mess.
Related
I have a DataGridView in a C# WinForms app that is DataBound at runtime (through Form_Load) to a custom Object.
In the design view of the DataGridView I have no columns set up.
When the Form loads the the columns are automatically created based on the data in the Custom object that it is DataBound to.
My question is how can I control the the Columns that are automatically created.
For example if I want one of the columns to be a DataGridViewLinkColumn instead of the DataGridViewTextBoxColumn that is automatically created?
The default columns are based on the data-type. I haven't checked, but for a link you could try exposing the data as Uri, but that might be hopeful. Really, if you want a specific type of column - add the columns through code and set DataGridView.AutoGenerateColumns to false.
As Andrew implies; normally something like reflection is used to generate the columns, and you'll get a column for every (browsable + public + readable) property. There is a layer of abstraction on top of this if you need, but this won't help with adding a hyperlink column.
You can pre-create your columns in the designer. If the name of the column matches the name of the property the column will end up bound to, the databinding will take care of the DGV population for you as before.
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.
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.
I have a GridView, I bind it to ObjectDataSource. ObjectDataSource takes data from a database's table. Each row in table has unique ID.
I have a button in each row of GridView that should remove this row from database. My ObjectDataSource returns Object, this returned object contains ID (and some other information, like name, user etc.) however I do not show this ID on my grid.
Question how can I get these ID after user chooses to delete row, I need to know what I should remove.
You should assign DataKeyName property of the grid view, once you do that, you can get value of your id that you have provided in .DataKey property, which is explain in detail here with source code
If you using BindingSource, than you always can get current object.
For example somewhere in mouse click handle:
var myData = (MyData)bindingSource.Current;
MyDataRepository.DeleteMyDataById(myData.Id);
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.