How to replace text in some cells of GridView after data binding? - c#

I have a GridView that is assosiated with database. Here's a data binding:
protected void GridViewProgramms_SelectedIndexChanged(object sender, EventArgs e)
{
int rowIndex = ((GridView) sender).SelectedIndex;
var programid = ((GridView) sender).Rows[rowIndex].Cells[1].Text;
GridViewEx.RowEditing += GridViewEx_RowEditing;
SqlDataSource1.SelectParameters["ID"].DefaultValue = programid;
GridViewEx.DataBind();
ExcersicePanel.Visible = true;
PanelAp.Visible = false;
}
Everything works fine, but I need to change some cells values in GridView after that. I need to rewrite every Cell in the last row. How to do this without affecting the database?

You will need to write your code(To change the cell text) in RowDataBound event of the gridview after data bind
Refer following link for more explanation
Update GridView Column after databinding

protected void GridViewEx_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
label or textbox ddltype = (label or textbox)e.Row.FindControl("id");
ddltype.text="ur text";
}
}

Related

How to show GridView's header and footer if all rows are deleted

I have GridView with CRUD operations, on load I'm adding one empty row with no data just to show the grid, after that I can add new rows, update, delete, but when I delete all rows my grid disappears and I lose Grid's header and footer.
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = new object[] { null };
GridView1.DataBind();
}
Is there any solution to set this row fixed?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem == null)
{
e.Row.Visible = false;
}
}
}
I see, you want your GridView to have headers and footers when there is no data inside of it. In ASP.NET 4.0+, they had introduced a attribute ShowHeaderWhenEmpty, You should set it to true if you want to display the header even of the GridView.

How to get different text for each button in same column dynamically in datagridview

I created a user control which has datagridview. Then I added row and column dynamically from textfile in datagridview.
My problem is I need column which has buttons in each row. In first row, the text of button is 'Test1' and in second row 'Test2' not the same text.
After searched on google I tried this code
var testButton = new DataGridViewButtonColumn();
testButton.Name = "Test";
testButton.HeaderText = "Test";
testButton.UseColumnTextForButtonValue = true;
testButton.Text = "Test1";
this.dataGridView1.Columns.Add(testButton);
But it gives me both button text as 'Test1'.
Assuming you are in Windows Forms, you need to add a DataGridViewButtonColumn to your DataGridView - Not directly to the DataTable.
This should occur somewhere after you bind the DataTable to the DataGridView.
Something like this should work:
DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.Name = "uninstall_column";
uninstallButtonColumn.Text = "Uninstall";
int columnIndex = 2;
if (dataGridViewSoftware.Columns["uninstall_column"] == null)
{
dataGridViewSoftware.Columns.Insert(columnIndex, uninstallButtonColumn);
}
Of course you will have to handle the CellClick event of the grid to do anything with the button.
Add this somewhere in your DataGridView Initialization code
dataGridViewSoftware.CellClick += dataGridViewSoftware_CellClick;
Then create the handler:
private void dataGridViewSoftware_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridViewSoftware.Columns["uninstall_column"].Index)
{
//Do something with your button.
}
}
//Change the Button text property on a data-bound event
protected void YourDataGridViewId_RowDataBound(object sender, GridViewRowEventArgs e)
{
// finding the button control..`enter code here`.
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button YourbtnVariable = (Button)e.Row.FindControl("YourbuttonID");
YourbtnVariable.Text = "Text"+e.Row.RowIndex+1;
//e.Row.RowIndex+1 will give u index of row every time incremented with 1
}
}

Is it Possible to Change the text color for columns in Grid View based upon search item?

Note: i need to change in different columns based upon search request.
Yes you can do it in GridView.RowDataBound event
Like this
protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].BackColor = Color.Beige;
// 0 could be any valid cell index in your row
}
}
for more info Go here

Skipping Items During Data Binding in gridview c#

how to skip specific item in row bind event C#
You could
Filter the data via the SQL query.
Filter the data via a table select if binding to a datatable/dataset
Filter data via a DataView
Use the RowDataBound event
Edit ~ Here is how to use the RowDataBound
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow) {
//A value to check for
string myValue = DataBinder.Eval(e.Row.DataItem, "myColumn").ToString();
if ((myValue == "a")) {
//Hide the row
row.Visible = false;
}
}
}
I am not sure how this will affect your paging....if you do have one.

asp.net gridview set format for unbound field

i have a gridview populated by the code below:
protected void CautaProiect_Click(object sender, EventArgs e)
{
wipDBTableAdapters.GetSummaryProiectTableAdapter proiecte = new wipDBTableAdapters.GetSummaryProiectTableAdapter();
SummaryGrid.DataSource = proiecte.GetData(CodProiect.Text);
SummaryGrid.DataBind();
}
The gridview will be populated with some columns with values.
The problem is that the values are formated like this 1234.5600 and i want them to be like 1,234.56
How ca i do this ?
You can format your data in the OnRowDatabound event
sample:
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label l = (Label)e.Row.FindControl("lblValue");
l.Text = String.Format("{0:C}", l.Text);
}
}
In your GridView columns, use the DataFormatString property and set it to the format you prefer. In your case, you'll want to use "N2".
A great cheatsheet of other formatting options can be found here.
you can use the following code to display in the gridview ItemTemplate
<asp:Label ID="lblFinalPrice" runat="server" Text='<%#Convert.ToDouble(Eval("FinalPrice")).ToString("#.00")%>'></asp:Label>
I have finally managed to find an answer for this :
Here is how :
protected void SummaryGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
double value = Convert.ToDouble(e.Row.Cells[4].Text);
e.Row.Cells[4].Text = value.ToString("#,#.##");
}

Categories