how to get selected cell value in gridview using asp..net - 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

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;
}

Non Gridview event capture edit mode row index

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;
}

How to hide Template Field Edit column in Gridview based on Gridview Row Count

I have a gridview, where i can add/delete rows. I dont want to give the delete option on the first row, that is I can delete any row but not the first one. How can i disable edit option for the first row ?
You can disable that cell from GridView.RowDataBound as you have used Template field, you can use FindControl method and then disable that cell..
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == 0)
{
//Disable that cell here..
}
}

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
}

Resources