Answer» Hi,
Ive created a datagrid completely programatically. however Im having trouble in CREATING a way fro the user to select a row when the page is viewed.
the datagrid itself diplays CONTACT information. the Database itself hold many fields for a particular contact. I only wish to display a couple of these, but then when the user selects a row they wil be displayed all the relavent contact info about that user.
Any Suggestions?
Ive included my code below
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { getData("", "fullName"); } public BoundColumn CreateBoundColumn(DataColumn c) { BoundColumn column = new BoundColumn(); column.DataField = c.ColumnName; column.HeaderText = c.ColumnName.Replace("_", " "); column.DataFormatString = setFormating(c); return column; } private string setFormating(DataColumn bc) { string dataType = NULL; switch (bc.DataType.ToString()) { case "System.Int32": dataType = "{0:#,###}"; break; case "System.Decimal": dataType = "{0:c}"; break; case "System.DateTime": dataType = "{0:dd-mm-yyyy}"; break; case "System.String": dataType = ""; break; default: dataType = ""; break; } return dataType; } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {
} protected void TextBox1_TextChanged(object sender, EventArgs e) {
} protected void Button1_Click(object sender, EventArgs e) { PlaceHolder.Controls.Clear(); getData(TextBox1.Text, DropDownList1.SelectedValue); } protected void getData(String search, String parameter) { DBConn myConn = new DBConn(); myConn.openConnection();
DataSet ds = new DataSet(); // Instantiate the data GRID control System.Web.UI.WebControls.DataGrid DataGrid1 = new System.Web.UI.WebControls.DataGrid();
// the GetDataSet method executes the Stored procedure and populates a dataset ds Name, jobTitle as Job Title, id as ID ds = myConn.ExecuteReturningDataset("SELECT fullName AS Name, jobTitle AS Job_Title, company AS Company, businessPhone AS Business_Phone, mobile AS Mobile, businessAddress AS Business_Address, email AS [E-mail], id FROM contactlist WHERE ("+parameter+" LIKE '%"+search+"%') ORDER BY fullName");
// the dataset is used as the data source for our newly created datatrid DataGrid1 DataGrid1.DataSource = ds; DataGrid1.AutoGenerateColumns = false; foreach (DataColumn c in ds.Tables[0].Columns) { DataGrid1.Columns.Add(CreateBoundColumn(c)); } DataGrid1.DataBind(); // DataGrid1 is added to the PlaceHolder PlaceHolder.Controls.Add(DataGrid1); myConn.closeconnection();
DataGrid1.Width = 1000; DataGrid1.GridLines = GridLines.Both; DataGrid1.CellPadding = 1; DataGrid1.ForeColor = System.Drawing.Color.Black; DataGrid1.BackColor = System.Drawing.Color.Beige; DataGrid1.AlternatingItemStyle.BackColo r = System.Drawing.Color.Gainsboro; DataGrid1.HeaderStyle.BackColor = System.Drawing.Color.Brown; } }hi,
how exactly do you want the user to select the row? Meaning the user clicks on a row and then you go to a new page where the user can edit the row and perform some kind of a database update on the data?
asp.net 2.0 has a built-in control for this, or are you using asp.net 1.1?Use the "Property Builder" link under the datagrids properties. Go to the "Columns" option. Uder "Text" enter 'Select'. Under "Command Name" enter 'Select'. Under "Button Type" select LinkButton. The select Column must be bound
You can select any row from the datagrid and it will be displayed as the highligted row in the grid.
The HIGHLIGHT row event is called SelectedIndexChanged event. The event is called when the select column is clicked. The select column can be added to the datagrid using the property builder explained above.
// This event is fired when the Select is clicked private void Select_DataGrid(object sender, System.EventArgs e) { // prints the value of the first cell in the DataGrid Label2.Text += myDataGrid.SelectedItem.Cells[0].Text; }
|