grid view and datagrid

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I want to be able to replicate this datagrid code for a gridview.
Basically I need to set the colour of my link buttons and set the text
depending upon the value of a text box :

public void dgUserList_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
//First, make sure we're NOT dealing with a Header or Footer row
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
{
LinkButton EditButton = (LinkButton)e.Item.Cells[9].Controls[0];
EditButton.ForeColor = Color.Blue;

LinkButton DeactivateButton =
(LinkButton)e.Item.Cells[10].Controls[0];
DeactivateButton.ForeColor = Color.Blue;

string strActive =
Convert.ToString(DataBinder.Eval(e.Item.DataItem,"Active"));

Any ideas would be appreciated.


CheckBox chkActive = (CheckBox)e.Item.FindControl("chkActive");

if (strActive == "True")
{
chkActive.Checked = true;
DeactivateButton.Text = "Deactivate";
}
else
{
chkActive.Checked = false;
DeactivateButton.Text = "Activate";
}
}
}
 
Firstly, rename your dgUserList object to gvUserList

public void dgUserList_ItemDataBound(object sender, DataGridItemEventArgs
e)
protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs
e)

if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
if (e.Row.RowType == DataControlRowType.DataRow)

LinkButton EditButton = (LinkButton)e.Item.Cells[9].Controls[0];
LinkButton EditButton = (LinkButton)e.Row.Cells[9].Controls[0];

CheckBox chkActive = (CheckBox)e.Item.FindControl("chkActive");
CheckBox chkActive = (CheckBox)e.Row.FindControl("chkActive");


The rest should be obvious from the above.
 
Thanks Mark...you couldn't help me convert this piece of code could you?
I'm having problems with the e.Item parts and the DataKeys stuff.

if (e.CommandName == "ActivateDeactivate")
{
int intActivate = 0;

CheckBox cb = new CheckBox();

cb = (CheckBox)e.Item.FindControl("chkActive");

if (cb.Checked.ToString() == "True")
{
intActivate = 0;
}
else
{
intActivate = 1;
}

int key = (int)GridView1.DataKeys[e.Item.ItemIndex];
SqlConnection con = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Xe
roxConnectionString"]);
con.Open();

SqlCommand cmd = new SqlCommand("update userlogin set Active
= " + intActivate.ToString() + " where userkey = " + key, con);
cmd.ExecuteNonQuery();
GridView1.EditItemIndex = -1;
BindData();
con.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top