onRowdatabound not giving label value in asp.net - c#

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.

Related

Make GridView AutGenerateColumn a LinkButton that fires RowCommand event

Simple question which my searches seem to be coming up blank on.
I have a GridView that is AutoGeneratingColumns=True
This generates a column "MyColumn". I would like to make this columns value a clickable LinkButton that fires the RowCommand event of the GridView with a specific CommandNmae and the cells value as the CommandArgument.
I'm trying to do this without using a custom ItemTemplate, I know how to do that. I'm hoping i can pragmatically modify a auto generated column in the GridView as described.
Tia
This can be done in the OnRowDataBound event of the GridView.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the current row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
///create a new linkbutton
LinkButton button = new LinkButton();
button.Text = "LinkButton";
button.CommandArgument = "MyArgs";
//assign a command to the button
button.Command += Button_Command;
//add the button to cell x
e.Row.Cells[3].Controls.Add(button);
}
}
Because you are adding control dynamically, the binding of the GridView should NOT be wrapped inside an IsPostBack check. Something you would do normally.

Gridview Label Appear in a Selected Row

I has a Gridview called Gridview1 which is binded to a datasource.
and I have a ItemTemplate at Column 5 within it contain a
- Linkbutton called search
- Label called lbl_password , visible press to true
- Label called txt_password , visible set to false
code in done in asp.net webForm , .aspx
when link button pressed this function will get the ID at cell 1 in the row tat button clicked
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
string fieldID = grdrow.Cells[1].Text;
Now i want to know how can I make the txt and lbl become visible when the button pressed
I am assuming you mean when link button pressed you want to make the text and label from the same row to be visible.For that purpose you can just use the findcontrol method on gridview row to find the control you need.
If that's what you want then in link button click event you can do as follow,
protected void lnkButton_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
((TextBox)grdrow.FindControl("yourtextboxID")).Visible = true;
((Label)grdrow.FindControl("yourLabelID")).Visible = true;
}
Here is more info on findcontrol.

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.

GridView column with checkbox field with SQL

I have GridView with 2 columns.
The first column is: test-label (TemplateField)
The second: checkbox (asp:CheckBoxField) that connect to sql table with bit column (done).
I want that on page load - the page will check every row, where the checkbox = true, the test-label.visble will be false.
I know how to write code with SELECT statement to check the value from the SQL table, but don't know how to check every row on the gridview on the page-load.
how can I do that?
(i can't use findcontroll for the checkbox because it's checkboxfield and not just "checkbox".
<asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" />
so, what can I do here? maybe to replace that field with regular cb? (i don't know how to do there databind - on the regular cb).
you can use GridView.RowDataBound Event
so you can do something like
protected void GVRowDataBound(object sender, GridViewRowEventArgs e)
{
var check = (CheckBox) e.Row.FindControl("ID"); // ID is id of the checkbox
var lable = (Label) e.Row.FindControl("LableID");
if(check != null && lable != null)
{
if(check.Checked)
{
lable.Visible = false;
}
}
}
You can't do it in Page.Load because the GridView isn't databound yet.
Try handling GridView.RowDataBound.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("checkbox");
Label lbl = (Label)e.Row.FindControl("test-label");
lbl.Visible = !(cb.Checked);
}
}

Cant get value of cell on gridview nested in formview

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();

Categories

Resources