I'm totally having trouble with a Gridview that's been stumping me all day. I don't use Gridviews very often so some of this is confusing me and Google hasn't been as much help as I was hoping.
I'm trying to bind an object created through LINQ to a gridview and then do some updating.
XAML:
<asp:GridView runat="server" ID="GV1"
onrowediting="gridViewUsers_RowEditing"
onrowcancelingedit="gridViewUsers_RowCancelingEdit"
onrowdeleting="gridViewUsers_RowDeleting"
onrowupdating="gridViewUsers_RowUpdating"
>
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
Code:
private void BindGrid()
{
List<Room> stuff = new List<Room>();
Hotel = dc1.Rooms.ToList();
GV1.DataSource = Hotel;
GV1.DataBind();
}
protected void gridViewUsers_RowEditing(object sender, GridViewEditEventArgs e)
{
GV1.EditIndex = e.NewEditIndex;
BindGrid();
}
I want to just grab the Room object from the selected row and update it back to the database through LINQ. But I can't seem to get my hands on that object. My next attempt was to grab each entry from the row. So I found code like the example below. But everything is null.
protected void gridViewUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string tenant = ((TextBox)(GV1.Rows[e.RowIndex].FindControl("Tenant"))).Text;
string age = ((TextBox)(GV1.Rows[e.RowIndex].FindControl("Age"))).Text;
string roomno = ((TextBox)(GV1.Rows[e.RowIndex].FindControl("Room_No"))).Text;
BindGrid();
}
Try to set AutoGenerateColumns = false and provide columns manually.
Something like:
<asp:GridView runat="server" ID="GV1"
onrowediting="gridViewUsers_RowEditing"
onrowcancelingedit="gridViewUsers_RowCancelingEdit"
onrowdeleting="gridViewUsers_RowDeleting"
onrowupdating="gridViewUsers_RowUpdating"
AutoGenerateColumns = "false"
>
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField HeaderText="Tenant" DataField="Tenant" />
<asp:BoundField HeaderText="Age" DataField="Age" />
<asp:BoundField HeaderText="Room_No" DataField="Room_No" />
</Columns>
</asp:GridView>
EDIT:
Please add this code to your solution
public static int GetCellIndexByFieldHandle(this GridView grid, string fieldHandle)
{
int iCellIndex = -1;
for (int iColIndex = 0; iColIndex < grid.Columns.Count; iColIndex++)
{
if (grid.Columns[iColIndex] is DataControlField)
{
DataControlField col = (DataControlField)grid.Columns[iColIndex];
if ((col is BoundField && string.Compare(((BoundField)col).DataField, fieldHandle, true) == 0)
|| string.Compare(col.SortExpression, fieldHandle, true) == 0
|| col.HeaderText.Contains(fieldHandle))
{
iCellIndex = iColIndex;
break;
}
}
}
return iCellIndex;
}
/// <summary>
/// Gets the ordinal index of a TableCell in a rendered GridViewRow, using a text fieldHandle (e.g. the corresponding column's DataFieldName/SortExpression/HeaderText)
/// </summary>
public static int GetCellIndexByFieldHandle(this GridViewRow row, string fieldHandle)
{
return GetCellIndexByFieldHandle((GridView)row.Parent.Parent, fieldHandle);
}
And try
string tenant = ((TextBox)GV1.Rows[e.RowIndex].Cells[GV1.Rows[e.RowIndex].GetCellIndexByFieldHandle("Tenant")].Controls[0]).Text;
Try this code to access your textbox values .
protected void gridViewUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridViewUsers.Rows[e.RowIndex];
Textbox txtbxtenant = (TextBox)row.FindControl("Tenant");
string tenanat = Convert.toString(tentant.text);
Textbox txtbxAge =(Textbox)row.FindControl("Age");
string age = Convert.toString(Age.text);
// string roomno = ((TextBox)
//(GV1.Rows[e.RowIndex].FindControl("Room_No"))).Text;
// BindGrid();
}
Related
I edited a DataGrid column so the location is now a DropDownList, which works fine. It populates the DropDownList from the database.
<asp:TemplateColumn HeaderText="Trailer Location">
<itemtemplate>
<asp:DropDownList ID="ddlTrailerLoc" runat="server" OnSelectedIndexChanged="ddlTrailerLoc_SelectedIndexChanged">
</asp:DropDownList>
<asp:HiddenField ID="hdlTrailerLoc" runat="server" Value='<%#Eval("TrailerLocation")%>' />
</itemtemplate>
</asp:TemplateColumn>
But when I change the value in the DropDownList I don't know how to save the changes made to the database.
protected void PopulateDDLs(DropDownList ddlTrailerLoc)
{
DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
if (dsTrailerLocation.Tables[0].Rows.Count > 0)
{
ddlTrailerLoc.DataSource = dsTrailerLocation;
ddlTrailerLoc.DataValueField = "Description";
ddlTrailerLoc.DataTextField = "Description";
ddlTrailerLoc.DataBind();
}
else
{
ddlTrailerLoc.Items.Insert(0, new ListItem("No Locations Entered", "0"));
}
}
protected void dgList_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager && e.Item.ItemType != ListItemType.Footer)
{
DropDownList ddlTrailerLocation = e.Item.FindControl("ddlTrailerLoc") as DropDownList;
if (ddlTrailerLocation != null)
{
PopulateDDLs(ddlTrailerLocation);
//set the value in dropdown
HiddenField hdlTrailerLoc = e.Item.FindControl("hdlTrailerLoc") as HiddenField;
if (hdlTrailerLoc != null)
{
ddlTrailerLocation.SelectedValue = hdlTrailerLoc.Value;
}
}
}
}
I tried creating this ddlTrailerLoc_SelectedIndexChanged method but the event doesn't run.
protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;
TableCell cell = list.Parent as TableCell;
DataGridItem item = cell.Parent as DataGridItem;
int selectedIndex = item.ItemIndex;
string selectedItem = item.Cells[0].Text;
// now save your work here and rebind the grid.
Trailer.UpdateTrailer(int.Parse(TrailerID), Company.Current.CompanyID,
txtTrailerReg.Text,
ddlTrailerLocation.Text);
ddlTrailerLocation.DataValueField = "Description";
ddlTrailerLocation.DataTextField = "Description";
ddlTrailerLocation.DataBind();
}
Please add AutoPostBack="True" in dropdown. It will work
<asp:DropDownList ID="ddlTrailerLoc" runat="server" OnSelectedIndexChanged="ddlTrailerLoc_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
Please write code as below
protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlTrailerLoc=sender as DropDownList;
if(ddlTrailerLoc!=null)
{
int trailerId=int.Parse(ddlTrailerLoc.SelectedValue.ToString());
//Save selected value in Database
// now save your work here and rebind the grid.
Trailer.UpdateTrailer(trailerId, Company.Current.CompanyID,
txtTrailerReg.Text,
ddlTrailerLoc.Text);
ddlTrailerLocation.DataValueField = "Description";
ddlTrailerLocation.DataTextField = "Description";
ddlTrailerLocation.DataBind();
}
}
I have a custom grid on which i have binded data in my c# code behind. I have given a hyperlink field to one of my column. If i click the hyperlink value, it should navigate to the details page of that hyperlink value. The code is given below,
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink myLink = new HyperLink();
myLink.Text = e.Row.Cells[2].Text;
e.Row.Cells[2].Controls.Add(myLink);
myLink.NavigateUrl = "Estimation.aspx?EstimateID=" + EstimateID + "&VersionNo=" + VersionNo;
}
}
If i click the link, the page is getting navigated, but i am not getting the details which are already pre-loaded in that page. Please give me suggestions on how to incorporate this.
Thanks
You can use this to redirect, read this
<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl="Default2.aspx">
HyperLink
</asp:HyperLink>
to add attribute with link just add
HyperLink1.Attributes.Add ("");
You need to do a small change in the RowDataBound event
myLink.Attributes.Add("href"," your url");
You need to fetch the values for EstimateID and VersionNo from the grid row data. Take a look at the documentation for GridViewRowEventArgs and you'll see there's a .Row property.
So your code needs to be something like:
myLink.NavigateUrl = "Estimation.aspx?EstimateID=" + e.Row.Cells[4].Text + "&VersionNo=" + e.Row.Cells[5].Text;
Or, maybe you need to get to the data item associated with the grid row, in which case take a look at e.Row.DataItem, the GridViewRow.DataItem property. This DataItem will need to be cast to the type of data you've bound to the grid in order to fetch the data from it, which might be something like:
((MyCustomDataRow)e.Row.DataItem).EstimateID
Try below solution :
Page-1 that is your list page :
ASPX code :
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind :
protected void Page_Load(object sender, EventArgs e)
{
List<Data> lstData = new List<Data>();
for (int index = 0; index < 10; index++)
{
Data objData = new Data();
objData.EstimateID = index;
objData.VersionNo = "VersionNo" + index;
lstData.Add(objData);
}
GridView1.DataSource = lstData;
GridView1.DataBind();
}
public class Data
{
public int EstimateID { get; set; }
public string VersionNo { get; set; }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink HyperLink1 = e.Row.FindControl("HyperLink1") as HyperLink;
HyperLink1.NavigateUrl = "Details.aspx?EstimateID=" + e.Row.Cells[1].Text + "&VersionNo=" + e.Row.Cells[2].Text;
}
}
Page-2 that is your details page :
Code behind :
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.QueryString["EstimateID"].ToString());
Response.Write(Request.QueryString["VersionNo"].ToString());
}
I want to bind my grid column header names by retrieving data from a table. This table has two fields, DomainID and DomainName, I want to display the DomainNames as Column header of the Grid.
Actually I am creating employee grid view. I want all the domain names of employee to be displayed as a header and i have to check the corresponding domain in Checkbox.
Please Give me some ideas.
Thanks in advance.
From what i understood....
Make a grid view
Create two columns:
a. TextBoxColumn
b. CheckBoxColumn
Set your column header using .HeaderText property
Add the columns to your data grid view
Query your database and get the data_table from it
using dgv.DataSource = data_table bind your data to the table
OR
Make a for loop for all rows in the data_table and add each row explicitly
For getting your checkboxes to work, handle the cellContentClick event of the data grid view and perform the necessary updates in your database.....
Hope it helps....
You could load the headers into a DataTable and then create them dynamically with a custom TemplateField.
Here's the aspx part:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" DataKeyNames="EmployeeID" runat="server" >
<SelectedRowStyle BackColor="Aqua" />
<Columns>
<asp:TemplateField HeaderText="Employee" SortExpression="Employee">
<ItemTemplate>
<asp:HiddenField ID="HiddenEmpID" Value='<%# Bind("EmployeeID") %>' runat="server" />
<asp:label runat="server" ID="LblEmployee" Text='<%# Bind("EmployeeName") %>'></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="BtnSave" Text="Save" runat="server" onclick="BtnSave_Click" />
Here's a complete sample:
public partial class GridTest : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
CreateGridColumns();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) BindGrid();
}
private void CreateGridColumns()
{
var tblDomain = GetDomains();
// Create dynamic TemplateFields
foreach (DataRow row in tblDomain.Rows)
{
String domainName = row.Field<String>("DomainName");
TemplateField field = new TemplateField();
//Initalize the DataField value.
field.ItemTemplate = new GridViewCheckBoxTemplate(ListItemType.Item, domaninName);
field.HeaderText = domainName;
//Add the newly created field to the GridView.
GridView1.Columns.Add(field);
}
}
private DataTable GetDomains()
{
var tblDomain = new DataTable();
tblDomain.Columns.Add("DomainID", typeof(int));
tblDomain.Columns.Add("DomainName");
tblDomain.Rows.Add(1, "Google.com");
tblDomain.Rows.Add(2, "Yahoo.com");
tblDomain.Rows.Add(3, "Msn.com");
tblDomain.Rows.Add(4, "Youtube.com");
tblDomain.Rows.Add(5, "Myspace.com");
tblDomain.Rows.Add(6, "Facebook.com");
tblDomain.Rows.Add(7, "Wikipedia.org");
return tblDomain;
}
private void BindGrid()
{
var tblDomain = GetDomains(); // load domains from database or wherever
var tblData = new DataTable();// load sample data
tblData.Columns.Add("EmployeeID", typeof(int));
tblData.Columns.Add("EmployeeName");
//add domains as DataTable-Columns
foreach (DataRow row in tblDomain.Rows)
{
String domaninName = row.Field<String>("DomainName");
//Add column from domain-name
tblData.Columns.Add(domaninName, typeof(bool)); //CheckBox-Checked is a boolean
}
//get some Employees and random checked state
var rnd = new Random();
var empRow = tblData.NewRow();
empRow["EmployeeID"] = 1;
empRow["EmployeeName"] = "Jon";
foreach (DataRow dom in tblDomain.Rows)
{
empRow[dom.Field<String>("DomainName")] = rnd.Next(0, 2) == 0;
}
tblData.Rows.Add(empRow);
empRow = tblData.NewRow();
empRow["EmployeeID"] = 2;
empRow["EmployeeName"] = "Eric";
foreach (DataRow dom in tblDomain.Rows)
{
empRow[dom.Field<String>("DomainName")] = rnd.Next(0, 2) == 0;
}
tblData.Rows.Add(empRow);
empRow = tblData.NewRow();
empRow["EmployeeID"] = 3;
empRow["EmployeeName"] = "Alain";
foreach (DataRow dom in tblDomain.Rows)
{
empRow[dom.Field<String>("DomainName")] = rnd.Next(0, 2) == 0;
}
tblData.Rows.Add(empRow);
GridView1.DataSource = tblData;
GridView1.DataBind();
}
// show how to retrieve all checkbox values and the according EmployeeID
protected void BtnSave_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0) return;
var checkBoxColumns = GridView1.Columns.Cast<DataControlField>()
.Select((bf,index) => new{Field=bf, Index=index})
.Where(f => f.Field.GetType() == typeof(TemplateField) && ((TemplateField)f.Field).ItemTemplate.GetType() == typeof(GridViewCheckBoxTemplate))
.ToArray();
foreach (GridViewRow row in GridView1.Rows)
{
int EmployeeID = int.Parse(((HiddenField)row.FindControl("HiddenEmpID")).Value);
foreach (var f in checkBoxColumns)
{
String domain = f.Field.HeaderText;
bool isChecked = row.Controls[f.Index].Controls.OfType<CheckBox>().First().Checked;
}
}
}
}
Here's the custom ITemplate:
public class GridViewCheckBoxTemplate : ITemplate
{
ListItemType _templateType;
string _columnName;
public GridViewCheckBoxTemplate(ListItemType type, string colname)
{
_templateType = type;
_columnName = colname;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_templateType)
{
case ListItemType.Header:
break;
case ListItemType.Item:
var chb1 = new CheckBox();
chb1.DataBinding += new EventHandler(CB_DataBinding);
container.Controls.Add(chb1);
break;
case ListItemType.EditItem:
//As, I am not using any EditItem, I didnot added any code here.
break;
case ListItemType.Footer:
break;
}
}
void CB_DataBinding(object sender, EventArgs e)
{
CheckBox chb = (CheckBox)sender;
GridViewRow container = (GridViewRow)chb.NamingContainer;
object dataValue = ((DataRowView)container.DataItem)[_columnName];
chb.Checked = dataValue != DBNull.Value && (bool)dataValue;
}
}
I have a datagrid in asp.net, with a boundfield. On the RowCommand event, I want to get the value of this boundfield. The boundfield, in the columns tag, loooks as below:
<asp:BoundField DataField="LoginID" HeaderText="LoginID" InsertVisible="False"
ReadOnly="True" SortExpression="LoginID" />
What would the accompanying C# be?
Thanks
In the Row_Command Event you can retrieve the index of the clicked Row in this way :
void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
//Check if it's the right CommandName...
if(e.CommandName=="Add")
{
//Get the Row Index
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row
GridViewRow row = ContactsGridView.Rows[index];
// Here you can access the Row Cells
row.Cells[1].Text
}
}
protected void gv_research_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "editResearch")
{
txt_researchName.Text = gv_research.Rows[index].Cells[1].Text.TrimEnd();
}
}
catch (Exception ee)
{
string message = ee.Message;
}
}
.aspx:
<asp:ImageButton ID="btn_Edit" runat="server" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' CommandName="editResearch" />
I am using Datagrid control to populate list of items/contents. I want to show what row has been selected, distinctive.
What property of the datagrid control should i use ?
How do i do this ?
Thanks
Ron.
<SelectedItemStyle BackColor="Pink" ForeColor="Green" />
The SelectedRow Property is perhaps what you need
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}
Ron, DataGrid hasnt default behaviour for row selecting. You should do it yourself:
<asp:DataGrid runat="server" ID="DataGridTest" SelectedIndex="1" OnItemCreated="DataGridTest_ItemCreated" DataSourceID="DataSourceTmp">
<HeaderStyle BackColor="HighlightText" Font-Bold="true" />
<ItemStyle BackColor="White" />
<SelectedItemStyle BackColor="#bbbbff" />
</asp:DataGrid>
Code behind:
public partial class _Default : Page
{
private const string DataGridSelectedRowCssClass = "selectedRow";
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(
GetType(),
"dataGrid_selectRow",
string.Format(
#"(function (dataGrid, $, undefined) {{
dataGrid.selectRow = function (row) {{
$(row).siblings('.{0}').css('background-color', '#{1}').end().css('background-color', '#{2}').addClass('{0}');
}}
}})(window.dataGrid = window.dataGrid || {{}}, jQuery);",
DataGridSelectedRowCssClass,
DataGridTest.ItemStyle.BackColor.ToArgb().ToString("X8").Substring(2),
DataGridTest.SelectedItemStyle.BackColor.ToArgb().ToString("X8").Substring(2)),
true);
}
protected void DataGridTest_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.SelectedItem)
{
e.Item.Attributes["onclick"] = "dataGrid.selectRow(this);";
if (e.Item.ItemType == ListItemType.SelectedItem)
{
e.Item.CssClass = string.Format("{0} {1}", e.Item.CssClass, DataGridSelectedRowCssClass);
}
}
}
}