WebPart and datagrid

Joined
Jul 12, 2006
Messages
2
Reaction score
0
Hi
I'm having some trouble with this WebPart I am making. I am using a datagrid, and I can't figure out how to retrieve the selected value from it. I can make the event fire, but I don't know how to get the value from the selected field. Code is as follows:

namespace WebPartLibrary
{
public class SearchCustomerWP : Microsoft.SharePoint.WebPartPages.WebPart
{
//create controls
DataGrid dgView;
ButtonColumn selectableLinks;

public SearchCustomerWP()
{
//Set WebPart properties
this.Title = "WebPart: Search customer";
this.ExportMode = WebPartExportMode.All;

//Instantiate controls
dgView = new DataGrid();
selectableLinks = new ButtonColumn();

//Set control properties
dgView.AutoGenerateColumns = false;
selectableLinks.HeaderText = "Name";
selectableLinks.ButtonType = ButtonColumnType.LinkButton;
selectableLinks.DataTextField = "Name";
selectableLinks.CommandName = "dgView_ItemCommand";

//Add links for selecting
dgView.Columns.Add(selectableLinks);

//Add event listener
dgView.ItemCommand += new DataGridCommandEventHandler(dgView_ItemCommand);

//Invoke method for filling datagrid
dgView_dataBind();
}

protected override void CreateChildControls()
{
this.Controls.Add(dgView);
ChildControlsCreated = true;
}

private void dgView_ItemCommand(object sender, DataGridCommandEventArgs e)
{
//Add label indicating the event fired
Label test = new Label();
test.Text = "DataGrid was clicked!";
this.Controls.Add(test);
}
}
}


I've left out the part with the method that sets the datasource (dgView_dataBind()), which is populated from a webservice. But the grid fills nicely and the event fires when I click the links. But I really need help on how to extract the value of the selected item. I am also uncertain if this is the best way to make this event fire, as a click on next or previous datagrid page link would also make it fire. Any comments are welcome :)

--
MrCrool
 
Joined
Jul 12, 2006
Messages
2
Reaction score
0
I found the answer.

Naturaly, you might say, you can find the value in the event argument.

private void dgView_ItemCommand(object sender, DataGridCommandEventArgs e)
{
//Add label with the selected value
Label selected = new Label();
selected.Text = e.Item.Cells[1].Text;
this.Controls.Add(selected);
}

The number in the cells array may vary depending on which column you want to get the value from.
 

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

Similar Threads


Top