Non Gridview event capture edit mode row index - asp.net

How can I get the index of a gridview row in edit mode from an event that is not a gridview event?
I have a DDL in a cell of the Gridview. When the DDL changes I want to perform an update to a label of another cell in the same row.
So I am using the DDL_SelectedIndexChanged Event and have everything working including the post to the label, but its always the frist row. What is the correct way to get the row index in edit mode from outside of a gridview event?
Thanks,

try this
protected void DDL_SelectedIndexChanged(Object sender, EventArgs e)
{
DropDownList ddlcontrol = (DropDownList) sender;
GridViewRow grrow = (GridViewRow) ddlcontrol.NamingContainer;
int rowindex = grrow.RowIndex;
}

Related

How to gain row value of button clicked in a radgrid's DetailTable

If I were to want to gain the value of a row in the master table in a template column button I would do:
" OnCommand=event>
Event
GridDataItem item = (GridDataItem)RadGrid1.Items[Convert.ToInt32(e.CommandArgumrnt)];
I am not sure what index the grid returns when the detailtable button is clicked and every time I try to access a column in that row I get an error saying column does not exist. I tried doing so:
GridDataItem item = (GridDataItem)RadGrid1.MasterTable.DetailTables[0].Items[Convert.ToInt32(e.CommandArgumrnt)];
This did not work as well.
Excuse me if I made any mistakes, I am on my mobile phone.
I have found a much simpler solution to my problem that doesn't if require me to use oncommand or command argument! Instead I use the simple Onclick.
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridDataItem item = (GridDataItem)btn.NamingContainer;
TableCell cell = (TableCell)item["UniqueName"];
string value = cell.Text;
}

Create textboxes and buttons in gridview

I have a gridview dynamicaly create from an ODBC datasource.
I want to make something like shopping cart so i want to add a column with textboxes for the quantity and some buttons to add to shopping cart the item in the row.
Is there any example?
Create a EventHandler for RowCreated , this will fire everytime a row is dynamically created
Something similar to this:
void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
TableCell actionsCell = new TableCell();
//put code here to add to actionsCell
// you might have to modify this to specify where in the row to insert
row.Cells.Add(actionsCell);
}
Then put your button or text or whatever you want in this dynamically created cell in the row

How to popluat value form database into combo box using Asp.net

I want to fetch list of students into combo box and on the base of combo box slection I will fetch their related record into another grid. please guide me regarding this task.
You can bind DropDownList as below
DropDownList1.DataSource=GetStudentDataSet();
DropDownList1.DataTextField="StudentName";
DropDownList1.DataValueField="StudentID";
DropDownList1.DataBind();
Put GridView bind code on selected change event of DropDownList as below
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Fecth selected student id
int studentId = Convert.ToInt32(DropDownList1.SelectedValue);
//Bind Grdivew
}

Find a GridView row programmatically

The row has a LinkButton that when clicked needs to highlight the row.
Code so far:
protected void linkbutton1_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
GridViewRow g = (GridViewRow)l.Parent; // what is the correct way to do this?
//g.Style etc etc
}
first of all set the "CommandName" property of LinkButton to "select",
then in the selectedIndexChanging event of gridview write below code:
for (int i = 0; i < GridView1.Rows.Count;i++ )
GridView1.Rows[i].BackColor = System.Drawing.Color.White;
GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Cornsilk;
Make use of the RowCommand event of the GridView instead of the Click event of the LinkButton.
Then you can have a CommandName on the LinkButton such as "HighlightRow" and do something like the following:
Select Case e.CommandName
Case "HighlightRow"
e.item.row.attributes("class") = "highlight"
End Select
Sorry its in VB.NET and not C#
1.) Set Command Name Property to "Select"
2.) Change style either on code behind as shown by #Raymond or give set Cssclass attribute for
SelectedRowStyle of gridview to CssClass="selecterowstyle"
.selectedRowstyle
{
background-color:#EAEAEA;
}

how to get selected cell value in gridview using asp..net

I am using a Link button in a GridView. I need the value of the button I have clicked. Is there any way to achieve this ?
I suppose you want to get related row from GridView
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
}
The required cell you can get from cells collection row.Cells

Resources