When user select a row in Telerik Rad Grid, i want to take fields in this row. how to do this?
It's a little tricky, but easy after you've done it once.
Step 1.
Go to the Radgrid itself and edit the field DataKeyNames="" (under MasterTableView) and add the datafield you are pulling:
<MasterTableView ... DataKeyNames="ColumnNameFromSqlGoesHere">
Step 2.
Decide how you are going to grab the values, on Row Change (SelectedIndexChanged) or on a buttong press with a command attached to it (ItemCommand).
If row change, per your question:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
var z = RadGrid1.SelectedItems[0].OwnerTableView.DataKeyValues[RadGrid1.SelectedItems[0].ItemIndex]["ColumnNameFromSqlGoesHere"];
}
This will assign the variable "z" to the value of the column you have chosen (ColumnNameFromSqlGoesHere) at that given row.
If you wish to select multiple variables every time you change row you need to add all the values you wish to select under the DataKeyNames=" ". (Seperated by commas). You would then fetch each value via the code seen in the SelectedIndexChanged method:
var a = RadGrid1.SelectedItems[0].OwnerTableView.DataKeyValues[RadGrid1.SelectedItems[0].ItemIndex]["SecondColumnGoesHere"];
var b = RadGrid1.SelectedItems[0].OwnerTableView.DataKeyValues[RadGrid1.SelectedItems[0].ItemIndex]["ThirdColumnGoesHere"];
Etc... You get the idea.
This should get you going. It is a solution straight from Telerik: Retrieving primary key values for selected items
Try this. This may help you.
STEP 1:Add one radiobutton column in the radgrid
STEP 1: Get the primary key of selected row in the radgrid.
int primaryKey =0;
RadioButton radioButton;
for (int i = 0; i < RadGrid1.Items.Count; i++)
{
radioButton = RadGrid1.Items[i].FindControl("rdSelect") as RadioButton;
If (radioButton.Checked)
{
primaryKey = RadGrid1.MasterTableView.Items[e.Item.ItemIndex]["ID"].Text;
}
}
Line in the if condition will be used to get the fields from the selected row just by changing the fields datakey name i.e. changing "ID" to other field
Read this article for more details...
http://codedotnets.blogspot.in/2012/01/get-primary-key-selected-radiobutton.html
Related
I have a Datagrid where a add items using grid.Items.Insert(0, row);
I always want the last row I insert to be displayed first on Grid.
I also want the 1st row to have different color from others. For this purpose I am using LoadingRow event :
private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
dataGrid.RowBackground = new SolidColorBrush(Colors.AntiqueWhite);//set all rows to default color
e.Row.Background = new SolidColorBrush(Colors.CornflowerBlue); //set current=>1st row row to BLue
}
However every row added is getting colored CornflowerBlue without previous added rows changed to AntiqueWhite.
I am missing something obvious, would really appreciate any guidance.
You need to loop through the rows in the datagridview and then compare values of columns 7 and 10 on each row (example columns).
Try this:
foreach (DataGridViewRow row in vendorsDataGridView.Rows)
if (Convert.ToInt32(row.Cells[7].Value) < Convert.ToInt32(row.Cells[10].Value))
{
row.DefaultCellStyle.BackColor = Color.Red;
}
Use Insert instead of Add. This way you can specify where you want to add the new record. To add a new record on top of the grid, just use Insert(0).
DataGridView1.Rows.Insert(0) // -- this will add a new record on top, always
DataGridView1.Rows(0).Cells(n).Value = "abc" // --this will set the value at n column.
I'm trying to do some error handling on a column to see if it's not equal to a certain word. I'm wondering though do i do this on the RowUpdating event or the RowUpdated event. I've been trying to do this in the rowupdating event and haven't had any luck so far. Here's the code.
TableCell cell = GridView1.Rows[e.RowIndex].Cells[3];
if(cell.Text == "hello")
{
e.Cancel = true;
}
lblError.Text = "hello" + cell.Text;
I assume the 3 after inside the brackets after .Cells is the column index that I want. If i use .Cells[1] it will show the primary key no in the label, but if I try 2 or 3 nothing seems to be getting placed inside the cell variable.
My table has four columns, ID, Name, Price, and CategoryID. Any help is appreciated.
If you are trying to get the input box values, you will have to find the control like:
GridViewRow row = GridView1.Rows[e.RowIndex];
string price = ((TextBox)(row.Cells[3].Controls[0])).Text;
You can also check against the e.NewValues.
And fyi: You will have to set the e.Cancel=true, if you want to cancel the event upon meeting certain condition(s).
Check RowUpdatingEvent ref.
I have a GridView with data. The first column on this GridView is the "SELECT" column.
If the user clicks on SELECT, it highlights the entire row.
I have an event on this click action:
protected void gvShows_SelectedIndexChanged(object sender, EventArgs e)
{
}
Basically what I want to do is for each SELECTED row, I want to extract the values for the columns:
dataSource
showId
episodeId
**Here's where I'm having problems:
string dataSource = "";
int showId = 0;
int episodeId = 0;
foreach (DataRow row in gvShows.Rows)
if (the row is selected/highlighted then...)
{
dataSource = the value under column "dataSrouce" for this ROW.
showId = the value under column "showId" for this ROW.
episodeId = the value under column "episodeId" for this ROW.
}
Can anyone give me a hand with this?
I believe you will want to use SelectedIndexChanging (and not SelectedIndexChanged) because that will give you access to the new selected row index. If the first cell is the button, the next three should give you the values you need:
protected void gvShows_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow r = gvShows.Rows[e.NewSelectedIndex];
string dataSource = r.Cells[1].Text;
int showId = Convert.ToInt32(r.Cells[2].Text);
int episodeId = Convert.ToInt32(r.Cells[3].Text);
}
Edit:
I wanted to add that you can use the SelectedIndexChanged if you didn't want to use SelectedIndexChanging:
protected void gvShows_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow r = gvShows.Rows[gvShows.SelectedIndex];
string dataSource = r.Cells[1].Text;
int showId = Convert.ToInt32(r.Cells[2].Text);
int episodeId = Convert.ToInt32(r.Cells[3].Text);
}
And if you are working with a database, you can add a datakey to the gridview, allowing you to pull the primary key for the record you want to work on.
Add the following to your gridview:
DataKeyNames="showId"
And then you can access this value from the codebehind:
int showId = Convert.ToInt32(gvShows.DataKeys[gvShows.SelectedIndex].Value);
You need to rely on the "selected/highlighted" rows' attributes. Find any attribute that could determine if it is a selected row.
EventArgs e should have a Row property that represents the selected row. Within there, you have access to each of the columns (called Cells). You get into each particular cell using indexing. So, if ShowId is in column 3, here's how you'd access it:
e.Row.Cells[2]; (remember, it's 0-based indexing in C#).
First to get ShowId, try e.Row.Cells[2].Text; That might get you what you need.
If that doesn't work, try this:
Now, within Cells[2], you have access to Controls, which again is a list of controls in that cell. Run your code, and put a breakpoint inside of SelectedIndexChanged. Then in your immediate window, type the code above, and hover your mouse over Cells[2] to open up the items in there. Hover again on Controls, and look at what's in there.
Usually there's a LiteralControl, then your control with ShowId, then another LiteralControl.
Validate that, and then you'll be able to access it like e.Row.Cells[2].Controls[1].Text or something like that.
I have a gridview (GridviewProduct) in that i Have a Template field column Link button as Remove From Cart and and another Template Field column next to it as Quantity and other three Fields are boundField like ProductName, ProductPrice and ProductBrand. Which Looks as Follows:
I want to move the quantity column to the end of the gridview at the right hand side. when i use this code it gives me an error:
Insertion index was out of range
var Quantity = GridViewProduct.Columns[1];
GridViewProduct.Columns.RemoveAt(1);
GridViewProduct.Columns.Insert(5, Quantity);
please help.
your requirement can be achieved in Gridview's RowCreated event
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
List<TableCell> columns = new List<TableCell>();
//Get the first Cell /Column
TableCell cell = row.Cells[1];
// Then Remove it after
row.Cells.Remove(cell);
//And Add it to the List Collections
columns.Add(cell);
// Add cells
row.Cells.AddRange(columns.ToArray());
}
If you have 5 columns and remove one of them, you have 4 left. The index of a new last column would than be 4 (since it is zero-based).
GridViewProduct.Columns.Insert(4, Quantity);
I am not sure if this removes all problems, but at least this error should go...
I am using a DataGridView to display some data in my application.
The data in the table gets changed dynamically according to the users input.
I am able to retrieve the data according to the user.
I have to add an extra column named ID and fill in the values serially starting from 1 to the number of rows which are generated dynamically.
I had added the column using dgrid.columns.add("UID");
But how to insert values at runtime?
Seeing your code, it is not correct to do:
dgrid.Columns.Add("UID");
You will have to do:
dgrid.Columns.Add("uidColumn", "UID");
To modify/add the value of an existing cell, if the row already exists, you can do:
dgrid.Rows[0].Cells["uidColumn"].Value = myValue;
That will modify the value of the column with name uidColumn and row 0. According to your problem, all you have to do is:
for (int i = 0; i < dgrid.Rows.Count; i++) {
dgrid.Rows[i].Cells["uidColumn"].Value = GetValueOfRow(i);
}
supposing that you have a method GetValueOfRow that receives a row index and returns the value you need in the ID column in that row.