Using <asp:GridView></asp:GridView> I am trying to display columns of database table. I want to fetch primary key of table but obviously don't want to display it. How can I achieve this?
This is my GridView
<asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF" AllowPaging="false" PageSize="5" AllowSorting="true" AutoGenerateColumns="false"
AutoGenerateEditButton="true" OnRowEditing="MyGrid_RowEditing" AutoGenerateDeleteButton="true" OnRowDeleting="MyGrid_RowDeleting"
OnRowDataBound="MyGrid_RowDataBound" EmptyDataText="No Value" BorderWidth="0px" BorderStyle="Solid">
<Columns>
<asp:BoundField DataField="PrimaryKey" HeaderText="UserId"/>
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
<asp:BoundField DataField="Column3" HeaderText="Column3" />
</Columns>
</asp:GridView>
I also tried visible=false to hide primary key, it hides primary key from displaying but also don't fetch its value and I want this value.
Hope my question is clear.
You need to set Visible = false within the OnRowDataBound event, this will mean the data is still accessible to you, but won't display on the page.
<asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF"
AllowPaging="false" PageSize="5" AllowSorting="true"
AutoGenerateColumns="false" AutoGenerateEditButton="true"
OnRowEditing="MyGrid_RowEditing" AutoGenerateDeleteButton="true"
OnRowDeleting="MyGrid_RowDeleting"
OnRowDataBound="MyGrid_RowDataBound" EmptyDataText="No Value"
BorderWidth="0px" BorderStyle="Solid">
<Columns>
<asp:BoundField DataField="PrimaryKey" HeaderText="UserId"/>
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
<asp:BoundField DataField="Column3" HeaderText="Column3" />
</Columns>
</asp:GridView>
In Codebehind:
protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
<asp:GridView ... DataKeyNames="PrimaryKey" />
You can use this:
<asp:BoundField DataField="PrimaryKey" visible="false" HeaderText="PKId"/>
Also use this
<asp:BoundField DataField="PrimaryKey" visible="false" HeaderText="UserId"/>
To get Primary Key
<asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF" DataKeyNames="PrimaryKey"
MyGrid.DataKeys[e.NewEditIndex].Value
OR
MyGrid.Rows[e.NewEditIndex].DataBoundItem
If you want to hide a column by its name instead of its index in GridView. After creating DataTable or Dataset, you have to find the index of the column by its name then save index in global variable like ViewStae, Session and etc and then call it in RowDataBound, like the example:
string headerName = "Id";
DataTable dt = .... ;
for (int i=0;i<dt.Columns.Count;i++)
{
if (dt.Columns[i].ColumnName == headerName)
{
ViewState["CellIndex"] = i;
}
}
... GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
{
int index = Convert.ToInt32(ViewState["CellIndex"]);
e.Row.Cells[index].Visible = false;
}
}
Related
I have an asp:linkbutton. When it's pressed I want to get a list of DetailID's based on which of the checkboxes in my GridView are checked. I have a lblTesting to see the list of DetailID's that I'm generating. My goal is to get a list of DetailID's that I pass to an edit page.
I have looked at several examples on Stack Overflow but I cannot get any value other than the checkbox is checked.
Here is the link I have at the top of my page:
<span style="float:right;"><asp:Linkbutton ID="getURLLink" runat="server" Text="Edit Die Detail" OnClick="GetLink"></asp:Linkbutton></span>
<asp:Label runat="server" ID="lblTesting"></asp:Label>
Here is my GridView:
<asp:GridView ID="DieListDetailGridView" runat="server" AutoGenerateColumns="false" OnRowDataBound="DieListDetailGridView_RowDataBound" DataKeyNames="DieID" HeaderStyle-HorizontalAlign="Center" HorizontalAlign="Center" ForeColor="#333333" GridLines="Both">
<RowStyle BackColor="#F5F5DC" />
<AlternatingRowStyle BackColor = "White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DieNum" HeaderText="Die Number" />
<asp:BoundField DataField="DieID" HeaderText="Die ID" Visible="false" />
<asp:BoundField DataField="DetailID" HeaderText="Detail ID" Visible="false" />
<asp:HyperLinkField DataTextField="DetailNum" HeaderText="Detail Number" DataNavigateUrlFields="DetailID" DataNavigateUrlFormatString="~/Tooling/DieDetail.aspx?DetailID={0}" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="MaterialType" HeaderText="Material Type" />
<asp:BoundField DataField="Hardness" HeaderText="Hardness" />
<asp:BoundField DataField="MinSpares" HeaderText="Min Spares" />
<asp:BoundField DataField="MaxSpares" HeaderText="Max Spares" />
<asp:BoundField DataField="CurrentSpares" HeaderText="Current Spares" />
<asp:BoundField DataField="DetailNotes" HeaderText="Detail Notes" />
</Columns>
</asp:GridView>
And my GetLink function:
protected void GetLink(object sender, EventArgs e)
{
// start off empty
string DetailIDList = "";
foreach (GridViewRow row in DieListDetailGridView.Rows) {
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkBox = (row.Cells[0].FindControl("chkBox") as CheckBox);
string DetailID = row.Cells[3].Text;
if (chkBox.Checked)
{
DetailIDList = DetailIDList + DetailID + ",";
}
}
}
// Remove the last , from the list
DetailIDList = DetailIDList.Remove(DetailIDList.Length - 1, 1);
// comma delimeted list of checked Detail ID's
lblTesting.Text = DetailIDList;
//Response.Redirect("~/Tooling/DieDetail.aspx?DetailIDList=");
}
I can't figure out why my DetailID is empty.
I've tried doing row.Cells[0].FindControl("DetailID") but that also didn't work.
What I am expecting to get from the DetailID are integers like "28925", "16423" etc.
I was asked to not show the column on the page, which is why visible = false. I still need to reference the ID to pass it to the edit page. I need to do the same edit to multiple Detail ID's.
What am I doing wrong?
The answer to having a list of ID's even though the datafield is set to not be visible was found on another StackOverflow question: ASP.NET: GridView value set to null when column is not visible
The answer was to remove the "Visible=false" from the GridView and on each row created (after the column has the data added for the row) make it not visible.
This code fixed my issue:
protected void DieListDetailGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
}
I have this GridView that fills on the Page_Load:
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
GridView1.DataSource = actBO.BuscarActividades();
GridView1.DataBind();
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" Visible="False" />
<asp:BoundField DataField="Class" HeaderText="Class" />
<asp:BoundField DataField="Day" HeaderText="Day" />
<asp:BoundField DataField="Time" HeaderText="Time" />
<asp:BoundField DataField="Vacants" HeaderText="Vacants" />
<asp:ButtonField ButtonType="Button" HeaderText="Book" Text="Book"/>
</Columns>
</asp:GridView>
Where column "Vacants" shows an int (it represents the amount of vacant booking spaces in a class).
Every row will have a button to book a specific class. I need to place a condition for when the field "Vacants" is zero, so the "Book" button will be disabled.
So far this is what it looks like: image.
As you can see, I need the button to be disabled when there are no more vacants. It shouldn't be able to be clicked.
To do so, you must register OnRowDataBound event. More explanations can be found in here.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" Visible="False" />
<asp:BoundField DataField="Class" HeaderText="Class" />
<asp:BoundField DataField="Day" HeaderText="Day" />
<asp:BoundField DataField="Time" HeaderText="Time" />
<asp:BoundField DataField="Vacants" HeaderText="Vacants" />
<asp:ButtonField ButtonType="Button" HeaderText="Book" Text="Book"/>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// get your button via the column index; ideally you could use template field and put your own button inside
var button = e.Row.Cell[5].Controls[0] as Button;
int vacant = 0;
var vacantVal = int.TryParse(e.Row.Cell[4].Text, out vacant);
if (button != null)
{
button.Enabled = vacant > 0;
}
}
}
Hope it helps.
I have a function that checks which sql query needs to be executed and return a DataTable, it is called searchData and is inside the CarsDataSet class
DataTable dataTable = null; //before there is code to detect the sql command to execute
if (sqlCommand != null)
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand, con);
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
}
return dataTable;
I am using then, this function on a aspx.cs page in order to bind this DataTable to a GridView..
GridViewCarsFiltered.DataSource = carsDataSet.searchData(type, manufacturer, maxPrice, minPrice, maxYear, minYear);
GridViewCarsFiltered.DataBind();
The GridView gets populated normally, but the problem comes when a try to get values of a row using an select index changed handler..
protected void GridViewCarsFiltered_SelectedIndexChanged(object sender, EventArgs e)
{
string carId = GridViewCarsFiltered.SelectedRow.Cells[1].Text;
}
I am receiving an 'ArgumentOutOfRangeException' saying that carId is null.
This is my GridView, the DataSourceID is null because I am setting that from code behind..
<asp:GridView ID="GridViewCarsFiltered" runat="server" CssClass="table table-striped table-hover" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataSourceID="" ForeColor="Black" OnSelectedIndexChanged="GridViewCarsFiltered_SelectedIndexChanged" DataKeyNames="Id">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
<asp:BoundField DataField="manufacturer" HeaderText="manufacturer" SortExpression="manufacturer" />
<asp:BoundField DataField="model " HeaderText="model " SortExpression="model " />
<asp:BoundField DataField="colour" HeaderText="colour" SortExpression="colour" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
<asp:BoundField DataField="year" HeaderText="year" SortExpression="year" />
<asp:BoundField DataField="location" HeaderText="location" SortExpression="location" />
<asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
</Columns>
</asp:GridView>
And this is a screenshot of the actual populated GridView
enter image description here
I hope I've been clear enough and that some of you can help me...
Thank you in advance
The preferred way to retrieve a value in this kind of situation is to use DataKeyNames instead of getting the value from the cell itself. This method can also be used to get values from fields that are not displayed in the columns of the GridView.
You can add the Id field to that attribute in the markup:
<asp:GridView ID="gv" runat="server" DataKeyNames="Id" ... >
and retrieve the value in code-behind this way:
protected void gv_SelectedIndexChanged(object sender, EventArgs e)
{
int carId = (int)gv.DataKeys[gv.SelectedRow.RowIndex].Values["Id"];
}
Note: I used gv instead of GridViewCarsFiltered only to reduce the width of the code samples.
UPDATE
On the other hand, if the GridView is empty after selecting the row, the problem could be that the ViewState is disabled (at the page level or in Web.config). Enabling it should resolve the issue.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" DataSourceID="EmptyL" AllowPaging="True"
AllowSorting="True" GridLines="Vertical"
OnRowUpdating="TaskGridView_RowUpdating">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:CommandField ShowEditButton="True" />
<asp:CheckBoxField DataField="Status" HeaderText="Status" SortExpression="Status"/>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="EmptyLeg" runat="server"
ContextTypeName="Dev.Orpheus.App_Data.DataStorageDataContext"
EnableDelete="True" EnableInsert="True" EnableUpdate="True"
EntityTypeName="" OrderBy="Id desc" TableName="EmptyL">
</asp:LinqDataSource>
protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label1.Text = ?????? updated ID ???????
}
Classical gridview data from linq
For example I update the field "Status"
How to get the ID of the updated object ?
You should get it in NewValues collection of GridViewUpdateEventArgs parameter
like this
e.NewValues["Id"]
for that you can use
<%# DataBinder.Eval(Container.DataItem, "Id") %>
method on CheckBoxField so that in
protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label1.Text = //Chechbox value
}
and can update database or other filed as per necessary
Have a Good day.
Label1.Text = e.Keys[0]
The grid contains a primary .Keys property.
I have a GridView like this
<asp:GridView ID="gv_FilesList" runat="server" AutoGenerateColumns="false" onrowcommand="gv_FilesList_RowCommand">
<Columns>
<asp:BoundField DataField="f_Id" Visible="false" HeaderText="File Name" />
</Columns>
<Columns>
<asp:BoundField DataField="f_Name" HeaderText="File Name" />
</Columns>
<Columns>
<asp:ButtonField ButtonType="Link" Text="Download" CommandName="DownloadFile" HeaderText="Download" />
</Columns>
</asp:GridView>
Now when I click on the download Button, how can I get the corresponding f_Id in order to get the related data from Database.
This code should work, tested on my local.
First, add DataKeyNames to your GridView.
<asp:GridView ID="gv_FilesList" runat="server" AutoGenerateColumns="false" onrowcommand="gv_FilesList_RowCommand" DataKeyNames="f_Id">
<Columns>
<asp:BoundField DataField="f_Id" Visible="false" HeaderText="File Name" />
</Columns>
<Columns>
<asp:BoundField DataField="f_Name" HeaderText="File Name" />
</Columns>
<Columns>
<asp:ButtonField ButtonType="Link" Text="Download" CommandName="DownloadFile" HeaderText="Download" />
</Columns>
</asp:GridView>
Then, access DataKeys from codebehind.
protected void gv_FilesList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DownloadFile")
{
//row index
int index = Convert.ToInt32(e.CommandArgument);
//retrieve f_Id
int f_Id = Convert.ToInt32(gv_FilesList.DataKeys[index].Value);
//download file with f_Id
DownloadFile(f_Id);
}
}
A couple of ways to skin it, but you could access it like below:
void gv_FilesList_RowCommand(Object sender, GridViewCommandEventArgs e) {
if(e.CommandName=="DownloadFile")
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gv_FilesList.Rows[index];
string fileDownloadId = row.Cells[1].Text;
//Pull from DB
}
And then add f_id, to the DataKeyNames attribute so it will store the hidden fields value.
<asp:GridView ID="gv_FilesList" runat="server" AutoGenerateColumns="false" onrowcommand="gv_FilesList_RowCommand" DataKeyNames="f_id">
DataKeyNames
A solution to your problem is described in this thread.
Basically you can access the row index in the event argument property called CommandArgument.