Grid view Row_Command issue - c#

my gridview Row command code looks like
protected void GridView_Admins_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView sourceGrid = sender as GridView;
int currentIndex = 0;
GridViewRow currentRow = null;
if (string.Compare(e.CommandName, "SHOW", true) == 0)
{
if (int.TryParse(e.CommandArgument.ToString(), out currentIndex))
{
currentRow = sourceGrid.Rows[currentIndex];
if (long.TryParse(sourceGrid.DataKeys[currentRow.RowIndex].Values["EmployeeID"].ToString(), out this.employeeId))
this.ShowAdministrativeRightsForUser();
}
}
}
I also have paging enabled in gridview. When I want to edit record I click on particular cell and i can edit records. However when I am on second page when i click on a cell to edit record, I get error at row currentRow = sourceGrid.Rows[currentIndex]; saying Index out of range.
What can be wrong?

Use this to get the index :
int index = Convert.ToInt32(e.CommandArgument);

Related

How to get the selected row index from an asp.net gridview row

I have a gridview with no datakeys. When I click the row edit button, I'm taken to the rowediting method. I've tried several ways to get the row index, but no luck.
protected void gvwCustomerLocation_RowEditing(object sender, GridViewEditEventArgs e)
{
var index1 = e.NewEditIndex; //gives me an incorrect index number
//The SelectedIndex always equals -1
for (int i = 0; i < gvwCustomerLocation.Rows.Count; i++)
{
if (gvwCustomerLocation.SelectedIndex == 1)
{
}
}
int index = gvwCustomerLocation.EditIndex; //The EditIndex = -1
GridViewRow row = gvwCustomerLocation.SelectedRow; //The SelectedRow = null
var test = row.RowIndex.ToString();
}
How can I get the selected row index?
I eventually gave up on getting a row index from my gridview and decided to make a primary key in my sql server result set to use as my key in the front end.

GridView: OnRowDatabound HeaderText Equals to ColumnText with AutoGenerateColumn True c# Asp.net

I have a gridview with first columns is same as headers i.e. same value with autogeneratecolumns as true, what i need to do in RowDatabound if HeaderText Equals to Intersect of the first Column text, Change the color to Yellow. Please see the attached image of the Desireed Gridview Output.
GridViewDesiredOutput
HTML
<asp:GridView ID="GvSamples" OnRowDataBound="GvSamples_RowDataBound" runat="server" AutoGenerateColumns="True">
C#
public void BindSamplesGrid()
{
DataTable _dt = new DataTable();
_dt = BL.GetSample(_conn);
GvSamples.DataSource = _dt;
GvSamples.DataBind();
}
protected void GvSamples_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text == e.Row.Cells[i].Text)
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace("_", " ");
}
}
}
I am using asp.net C# Gridview.
Thank you
There are a couple of issues with your code. First of all you can only access the header row in DataControlRowType.Header, so coloring the cells as desired must be done in DataControlRowType.DataRow.
Next you are evaluating exactly the same value, so it will always be true: e.Row.Cells[i].Text == e.Row.Cells[i].Text, although this does not seem to do much except replace a _, which has nothing to do with getting the cell color to be yellow.
The below snippet does what you need. It gets the current row number and colors the correct cell yellow.
protected void GvSamples_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//get the current row number
int rowIndex = e.Row.RowIndex;
//check if the rownumber does not exceed the column count
if (rowIndex < e.Row.Cells.Count - 1)
{
//make the cell yellow
e.Row.Cells[rowIndex + 1].BackColor = Color.Yellow;
}
}
}

Add and find Dropdownlist at Runtime inside GridView

In my asp.net application, I have used Gridview control, In which i have to add Dropdownlist at runtime for each cell.Which i am able to bind successfully.
Below is my code which inside row databound event,
foreach (GridViewRow row in gdvLocation.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
for (int i = 1; i < row.Cells.Count; i++) {
var dlRouteType = new DropDownList();
dlRouteType.ID = "ddlRouteType";
dlRouteType.DataSource = GetRouteTypeList();
dlRouteType.DataTextField = "RouteType";
dlRouteType.DataValueField = "Id";
dlRouteType.DataBind();
row.Cells[i].Controls.Add(dlRouteType);
}
}
}
I have a button in my page, which has functionality to save data to database . While saving data i have to pass the value from Dropdownlist which i have added at runtime. On button click i am writing following code to get data from dropdownlist,
var ddlDropDown = (DropDownList)row.Cells[i].FindControl("ddlRouteType");
But i am getting null in ddlDropDown object. I have even added Update panel inside aspx page. Any suggessions most welcome.
Thanks in advance
Sangeetha
You have these errors in your code
RowDataBound already iterates through each rows and so all you need not write that foreach on top
You are iterating from index 1, index are zero-based. So start from zero.
The DropDownList ID must be unique, so better write something like,dlRouteType.ID = "ddlRouteType_" + i;
The code should be,
protected void gdvLocation_RowDataBound(object sender, GridViewRowEventArgs e)
{
//removed the foreach loop
var row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < row.Cells.Count; i++) //changed index
{
var dlRouteType = new DropDownList();
dlRouteType.ID = "ddlRouteType_" + i; //gave unique id
dlRouteType.DataSource = GetRouteTypeList();
dlRouteType.DataTextField = "RouteType";
dlRouteType.DataValueField = "Id";
dlRouteType.DataBind();
row.Cells[i].Controls.Add(dlRouteType);
}
}
}

How to assign a ToolTip for the first column data in the GridView using a ListBox

I have a ListBox with number of items matching the number of rows in the GridView. Using this ListBox I want to display a tooltip for each row only to the first column data in the GridView. I have bound the GridView in the front end.
The code I have tried is giving Index of out range error :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i <= GridView1.Rows.Count; i++)
{
String ProCol = GridView1.Rows[i].Cells[0].ToString();
if (ProCol.Length != 0)
{
e.Row.Cells[0].ToolTip = ListBox1.Items[i].ToString().Trim();
}
}
}
}
It fails in the last iteration of your for cycle, since the index is zero based (first item has 0 index, last item has count-1 index). Replace
i <= GridView1.Rows.Count
with
i < GridView1.Rows.Count
Try moving your code to the DataBound event on the GridView instead of doing the work in RowDataBound. That way, it will only run once when all the rows have been bound and the GridView.Rows collection has been initialized.
Also, you should do what Michal Klouda wrote in his answer regarding <= an <.
And you must make sure that the ListBox1 has been databound before you bind the GridView.
Sample code that should work as long as you bind the ListBox befor the GridView:
protected void GridView1_DataBound(object sender, EventArgs e)
{
var gv = (GridView)sender;
for (int i = 0; i < gv.Rows.Count; i++)
{
var oneRow = gv.Rows[i];
String ProCol = oneRow.Cells[0].ToString();
if (ProCol.Length != 0)
{
oneRow.Cells[0].ToolTip = ListBox1.Items[i].ToString().Trim();
}
}
}

How to change the index of previous selected Dropdownlist?

I have gridview with template field of dropdownlist in each row.when i select any value from dropdownlist it will change color of 3rd row below than it.But when i click on another dropdownlist it is changing the color of 3rd row below than it but the index of previous dropdownlist is not changed to 0 i.e"Select".it is still showing the value selected in last dropdown list.I just want when i click on a dropdownlist ,all other dropdownlist should be indexed to 0 i.e "Select" except one which i have clicked.
I am doing following in Selectedindexchange event of dropdownlist:
protected void GridView1_SelectedIndexChanged(object sender,EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
int g1 = gvRow.RowIndex;
GridView1.Rows[g1].BackColor = Color.White;
}
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
int g = row.RowIndex + 3;
int current_row_index = row.RowIndex;
int pp = GridView1.Rows.Count;
GridView1.Rows[g].BackColor = Color.Red;
}
}
Thanks in advance.
Just use DropdownName.ClearSelection() for each of your other dropdown except the one where you are calling Selectedindexchange event.
Maybe something like this:
protected void GridView1_SelectedIndexChanged(object sender,EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
int g = row.RowIndex + 3;
int current_row_index = row.RowIndex;
foreach (GridViewRow gvRow in GridView1.Rows)
{
gvRow.BackColor = Color.White;
if (gvRow.FindControl("nameOfTheDropdown") != null && gvRow.RowIndex != current_row_index)
{
((DropDownList)gvRow.FindControl("nameOfTheDropdown")).SelectedIndex = 0;
}
}
GridView1.Rows[g].BackColor = Color.Red;
}
Regards

Categories

Resources