Cant get value of cell on gridview nested in formview - c#

I have a formview and a nested gridview where i would like to be able to select the value of a specific cell when the row has been updated. I took the example code from the msdn site as it's close to what i want:
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
GridView SprayGrid = (GridView)FormView1.FindControl("GridView1");
int Index = SprayGrid.EditIndex;
GridViewRow row = SprayGrid.Rows[Index];
TextBox message = (TextBox)FormView1.FindControl("TextBox1");
message.Text = row.Cells[4].Text;
}
However, for some reason this will only pick up the index of the cell, i.e. if i change the row.cells[0] i can see in my message box the index number if i want to see any other cell then the message is blank? Any ideas would be great.

Use DataKeys on the GridView, and instead of trying to set the TextBox value from the cell, set it from the DataKey.
In the ASPX:
<asp:GridView Id="GridView1" runat="server" DataKeyNames="SomeColumnName">
And in your row updating event:
TextBox message = (TextBox)FormView1.FindControl("TextBox1");
message.Text = SprayGrid.DataKeys[row.RowIndex]["SomeColumnName"].ToString();

Related

onRowdatabound not giving label value in asp.net

I am creating the dynamic grid and adding textbox and label in row dynamically on some button click and bind some data from the database to textbox and label.I want to change the label text color and text box read only for condition depend on database.i have used onRowDataBound event of gridview but not getting any value in the textbox and label for a row. Can anyone helps me to solve this issue? Thanks
protected void grdMasterData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label status = e.Row.FindControl("lblProduct") as Label;
if (status.Text == "LY Actuals")
{
e.Row.Cells[0].CssClass = "lblProductColor";
// lbtAction.Visible = false;
}
}
}
In RowDataBound three row types provided by grid, you are using only DataRow, try to get your values from empty row and footer row because first record will not come in DataRow.

How to select a row in a DataGridView programmatically AND trigger DataGridView.SelectionChanged event?

For the life of me I cannot seem to figure this out. I have a long DataGridView (that does not allow MultiSelect) and when a user commits a change to the data, the data from the grid is purged and redrawn (because changes can affect multiple rows, this was the simpler approach). However, when I try to select the row programmatically, it does not also fire the DataGridView.SelectionChanged event, which I use to display data from an array which is correlated to the DataGridView current cell index. When doMagicStuff executes, the values for the wrong index (specifically, index 0) is show.
private void doMagicStuff()
{
int selRow = myDGV.CurrentCell.RowIndex;
myDGV.Rows.Clear();
/*Perform Task, Redraw data*/
myDGV.CurrentCell = myDGV[selRow, 0];
}
private void myDGV_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = myDisplayValue1[myDGV.CurrentCell.RowIndex];
Label2.Text = myDisplayValue2[myDGV.CurrentCell.RowIndex];
TextBox1.Text = myEditValue1[myDGV.CurrentCell.RowIndex];
TextBox2.Text = myEditValue2[myDGV.CurrentCell.RowIndex];
}
Make sure that your client settings and OnSelectedIndexChanged is set like so: (ASP.NET AJAX)
.aspx page
<telerik:RadGrid ID="Grid1" runat="server" OnSelectedIndexChanged="Grid1_SelectedIndexChanged" OnItemDataBound="Grid1_ItemDataBound" OnPreRender="Grid1_PreRender">
<ClientSettings EnablePostBackOnRowClick="true">
<Selecting AllowRowSelect="true"></Selecting>
</ClientSettings>
</telerik:RadGrid>
aspx.cs page
protected void Grid1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = null;
foreach(GridDataItem item in Grid1.SelectedItems)
{
//column name is in doub quotes
value = item["Name"].Text;
}
}
Add a button click to the form to test the selected values in the DataGridView.. double click that button then paste this code in there
foreach (DataGridViewRow row in myDGV.SelectedRows)
{
Label1.Text = //This should be hard coded the only thing that should change dynamically is the TextBox Values
Label2.Text = //This should be hard coded the only thing that should change dynamically is the TextBox Values
TextBox1.Text = row.Cells[0].Value.ToString();//change the 0 or 1 to fit your column Index position
TextBox2.Text = row.Cells[2].Value.ToString();
}
also if you have 4 columns and 4 text boxes then you will assign all of the textbox.Text values within the foreach loop just follow the pattern and increase the index by 1 so 2 textboxes means row.Cells[0] is the first column row.Cells[1] is the second column ...etc

How to get a cell value or row value in ControlGrid Level 1 GridView C# - DevExpress

i wonder if how can i get the value of the cell in ControlGrid Level 1 GridView.
i had already tried the click event with this code
private void gvPrerequisit_Click(object sender, EventArgs e)
{
MessageBox(gvPrerequisit.GetFocusedRowCellValue("ProspectusPrequisitID").ToString());
}
but on debugging mode this message appear.
System.ArgumentNullException: Value cannot be null.
Here's the screenshot on the control grid.
Then the cell to get the value.
Anyone can help? thanks.
First you have to cast the GridView
On click event GridView1
GridView gv = (GridView)GridView1.GetDetailView(GridView1.FocusedRowHandle, 0);
so this gv is now your GridView2
then you can call the value by this...
gv.GetFocusedRowCellValue("ColumnName"))

Textbox control from gridview column is returned but gives empty value

I have succeeded in retrieving Textbox from textbox column of Gridview, but it can't retain the value I have put there. It simply returns reference to textbox but miss the encapsulated value; Following is sample code I'm using to retrieve the textbox and its value; Please help me out of handling such problem.
protected void Submit_RowCommand(object sender, GridViewCommandEventArgs e)
{
int RowIndex = int.Parse(e.CommandArgument.ToString());
string userId = ApprovalParentGridView.DataKeys[RowIndex]["UserID"].ToString();
GridViewRow row = ApprovalParentGridView.Rows[RowIndex];
TextBox remarks = (TextBox)row.Cells[6].FindControl("txtRemark");
string remarks = remarks.Text.Trim();
...
}
Thanks in advance!!!
I have had this in the past and found it to be because the Gridview is loaded in Page_Load and the textbox had its value set in that routine, and Submit_RowCommand is fired afterwards.
You need to rebuild the gridview so that the control is in the controls collection but you need to be careful that you do not put the value in the textbox at the same time or else the code behind will overwrite the value that you user has entered from their browser.

ModalPopup_Extender.Hide() Issue

I have a button, when clicked, a modalpopup with gridview inside it. as below;
protected void grdDetails_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grdDetails.SelectedRow;
//Note: Value of that row's cell must display in A text box (txtBox) on main page
txtBox.Text = row.Cells[2].Text;
ChargeFilterModalDialogExtender.Hide();
}
After the extender is hide, the value of the cell not displaying in the text box. Am I doing something wrong?
Where you have the textbox?
Whether you are using UpdatePanel?, if so define the needed asynchronous postback triggers.

Categories

Resources