Add links to all cells in gridview - c#

I have a data source with an unknown number or rows and columns. I am using a grid view which is set to auto generate columns. I need to turn each item in each cell into a link button to post back for processing. I know how to dynamically add a control to a known row and cell but when i don't know the column name it makes it difficult. Any suggestions on how to do produce these results?

Try this as a starting point:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
HyperLink myLink = new HyperLink();
myLink.NavigateUrl = "somewhere.aspx";
if (cell.Controls.Count > 0)
{
while (cell.Controls.Count > 0)
{
myLink.Controls.Add(cell.Controls[0]);
}
}
else
{
myLink.Text = cell.Text;
}
cell.Controls.Add(myLink);
}
}
}
Note: I've written the solution up in C#, as per your tag, but I notice your last comment is in VB. Let me know if you need me to re-post in VB (in which case you should update the tag).

Related

How to add an additional cell in gridview showing total number of rows?

I need to put the total number of rows in a cell at the bottom of an asp.net gridview. I can do so far is showing it outside the gridview, but the requirement is to show it at the bottom of the gridview itself. How to achieve that?
Hi you need to define variable for record count as RowNumber and define lable to display number of row at footer and refer below code, it may help you.
private int RowNumber=0; // Define at the top
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RowNumber += 1;
}
// you need to add one label at Footer section
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotalRow = (Label)e.Row.FindControl("lblTotalRow");
lblTotalRow.Text = RowNumber.ToString();
}
}

How to change the color of label depending on certain values in the gridview

I have a sqldatasource and gridview.
I have two columns:
ID | Status
1 Closed
2 Opened
3 Waiting
How to change the color of the label in the view state of the gridview depeding on the value from the 'status' column.
For example if the value in the cell is "Closed" the label color will be red , if it's opened then it will become green and so on.
I've thought about looping through all cells of the status column and if the cell contains a certain value the color will change. ( in row data bound event ) . But I haven't done this because I don't find this idea as a good one because of the looping part.
use the RowDataBound event from the grid view to check what is the status. You don't need to do a loop because that event (if you register or not) is being called.
One thing to note, you will need to make sure you are looking at the right row (header, footer, alternate etc) so something like this
void YourGridViewName_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Do your color change here by accessing the col with e.Row.Cells[column_index].BackColor = 'what ever you want'
}
}
You can enable the RowDataBound Event in the markup
<asp:GridView ID="gridview1" runat="server" OnRowDataBound="RowDataBound">
</asp:GridView>
And put this in your Code-Behind file.
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String state = rowView["state"].ToString();
//format color of the as below
if(state == "Closed")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Red;
if(state == "Open")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Green;
if(state == "Waiting")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Yellow;
}
}
You have to write code in the rowdatabound event of your grid view.
Example:
private GridView1_RowDatabound(object sender,EventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// You have to put your logic here.
if( e.Row.Cells[1].Text == "closed" )
{
// to get a reference to label control
Label lb = e.Row.FindControl("LabelCOntrolID");
}
}
}

How to check if value from a cell in a column is !=NULL?

I'm trying on gridview updated to check if a cell from a certain column is != NULL ( to check if the user wrote something into the cell)
My problem is I don't know how to get the "x column" value from cell.
inzi irina Please look at this code. don't forget to vote me if this helps you
private void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
string lngth = Convert.ToString(dr.Cells[1].Value);
if (lngth.Length > 0)
{
listBox1.Items.Add(dr.Cells[0].Value);
}
}
}
I assume you are approaching through following ways...
You have a footer template and may have a button which saves the values from your footer.
You have an Edit/Update button in grid for each row
For Approach 1(i.e. Footer Template)
You can find like below...
TextBox testing = (TextBox)grd.FooterRow.FindControl("Your Control ID");
For Approach 2 (i.e. using Edit/Update Button)
You can do like below..
Sample Code Behind
protected void grd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox testing = (TextBox)grd.Rows[e.RowIndex].FindControl("Your Control ID");
}
Sample HTML
<asp:GridView ID="grd" runat="server" onrowupdating="grd_RowUpdating">
</asp:GridView>

unable to change Column names when populating with list and using Auto-generate fields

The default column names are item1, item2 and item3 and I'm unable to change these, I've seen a similar question on here but none of the solutions seemed to work.
if (depositTotal != cartTotal & depositTotal != 0.0)
{
list.Add(Tuple.Create(amazonOrderID, depositTotal, cartTotal));
}
dataGridTotals.DataSource = list;
dataGridTotals.DataBind();
I've tried using this after and before the gridview data is bound but it doesn't work:
dataGridTotals.Columns[0].HeaderText = "New Header for Column";
Has anyone got any other ideas?
Autogenerated columns do not populate the columns list. You will need to loop through the cell controls of the first row and manually change the text.
Use this method for your grid's RowDataBound event. It will catch the header row and reassign text values. This code assumes you know the order in which your columns will be received. If not, you can read the text of each row and act accordingly...
private void gvMyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Apply to header only.
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Column 1 Text";
e.Row.Cells[1].Text = "Column 2 Text";
e.Row.Cells[2].Text = "Column 3 Text";
}
}
The above code assumes you know the order in which your columns will be received. If not, you can read the text of each row and act accordingly:
private void gvMyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Apply to header only.
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (Cell cell in e.Row.Cells)
{
if (cell.Text == "FName")
{
cell.Text = "First Name";
}
else if (cell.Text == "LName")
{
cell.Text = "Last Name";
}
else if (cell.Text == "Etc")
{
cell.Text = "Et cetera";
}
}
}
}
In this latter case, you would want to setup a dictionary of SQL field names and friendly English names to read so you don't have fifty if statements, but you get the idea.

C# dynamically taking data from DataGridView

I'm trying to get my current program to take information from a dynamically created DataGridView. I have managed to get the information into the grid, and perform the required search, however now I'm really stuck.
I have added a column to the datagridview which holds a button within each row. What I'd like to do is take the value of the data from column index 1 which is in the same row as the button clicked. Confusing? Anyway, here's the code:
public void GetValues(...)
{
//Details regarding connection, querying and inserting table
.
.
.
DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();
buttonCol.Name = "ButtonColumn";
buttonCol.HeaderText = "Select";
buttonCol.Text = "Edit";
//NB: the text won't show up on the button. Any help there either?
dataGridView1.Columns.Add(buttonCol);
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewButtonCell button = (row.Cells["ButtonColumn"] as DataGridViewButtonCell);
}
dataGridView1.Columns["ButtonColumn"].DisplayIndex = 0;
}
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//Here is where I'm having the trouble. What do I put in here???
}
Thanks for any help you can give!
David.
Your DataGridViewCellEventArgs contains very useful information such as RowIndex.
So something like (I don't know what you want to do with the value):
String dataYouWant = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
`
if (e.ColumnIndex != button_column_number) //column number of the button.
return;
dataGridView1.EndEdit();
bool val;
if ((dataGridView1.Rows[e.RowIndex].Cells[1].Value) != null) // column index 1...as that's what you want.
{
//d stuff you want here.
}
else
{
}
`

Categories