Gridview showing ID, Can I change it so it displays the Description?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey Guys,

Have a tricky question here. I have a gridview, that works fine.
When I click edit, it shows the Drop down list and I can select from a table in my database and update it to that Items value.
But this only shows the Items ID, not the description. See below.
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/119595
The table it shows is supposed to be DeviceType. Its linking perfectly, just its not showing the item name as it should.
The table looks like this.
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/119600
This is all done by code and not through the wizard. The wizard doesnt do what I want. I have tried many stored pros and still no look. They display the info perfectly but I cannot update or edit. So I left them.

Here is the ASP code

<pre class="prettyprint <%@ Page Title = "Show Equipment" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ShowEquipment.aspx.cs" Inherits="ShowEquipment" %>

<%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent

<style type="text/css
body
{
background-image:url(RBKSmall.png);
}
.All
{
height: 268px;
width: 918px;
}
</style>
</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onrowdatabound="GridView1_RowDataBound
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Device Type
<ItemTemplate >
<asp:Label ID="Label2" runat="server" Text=<%# Eval("DeviceType") %>></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:DropDownList ID="DropDownList1" runat="server" >

</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="Edit" ShowHeader="false
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit" ></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update" ></asp:LinkButton>
<asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel </asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>



</asp:Content>



[/code]
<br/>
And here is the C# code.

<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.UI.Adapters;

public partial class ShowEquipment : System.Web.UI.Page
{
//Connection String taken from my Web.Config File
string con = ConfigurationManager.ConnectionStrings["MarksDatabaseConnectionString"].ConnectionString;
SqlConnection sqlcon;

public void Page_Load(object sender, EventArgs e)
{


if (!this.IsPostBack)
{

showgrid();
}
}

public void showgrid()
{
DataTable dt = new DataTable();
sqlcon = new SqlConnection(con);

sqlcon.Open();
SqlDataAdapter sda = new SqlDataAdapter();
string strQuery = "select * from Equipment ";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
showgrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
showgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
sqlcon = new SqlConnection(con);
sqlcon.Open();

string sql = "update Equipment set DeviceType=" + ddl.SelectedValue.ToString() + "";

SqlCommand cmd = new SqlCommand(sql);
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
cmd.ExecuteNonQuery ();
GridView1.EditIndex = -1;
showgrid ();
}

public DataTable load_devicetype()
{
DataTable dt = new DataTable();
sqlcon = new SqlConnection(con);
sqlcon.Open();
string sql = "select * from DeviceType";
SqlCommand cmd = new SqlCommand(sql);
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(dt);
return dt;

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dp= (DropDownList )e.Row .FindControl ("DropDownList1");
DataTable dt = load_devicetype();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem lt = new ListItem();
lt.Text = dt.Rows[0].ToString();
dp.Items.Add(lt);
}
dp.SelectedValue = drv[3].ToString();

}

}
}
}
[/code]
<br/>
If you need any more info please let me know. The database is a relational one, with one major table linked to many smaller tables.
Thanks in advance.
Mark.

View the full article
 
Back
Top