Hi Friends,
I have a combobox with two items that is "Important" and "Very Important" and a textbox for 'short note'. I have saved those to database. I want the data to load on gridview but with colored row. I mean for the data that is 'Important' i want the row to be colored 'blue' and for the data that is 'very important' i want the row to be colored 'red'. How can it be done.
I anyone has solution, please post it
thanks
prasanna
Loading
Satyapriya NayakPosted Nov 30, 2011, 12:48 PM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add("Select");
DropDownList1.Items.Add("Important");
DropDownList1.Items.Add("very Important");
}
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "insert into Details(shortnote,flag) values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
bindgrid();
}
void bindgrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from Details";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "Details");
GridView1.DataSource = ds;
GridView1.DataMember = "Details";
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "flag")) == "Important")
{
e.Row.BackColor = System.Drawing.Color.Blue;
}
else
{
e.Row.ForeColor = System.Drawing.Color.Red;
}
}
}
}
Thanks
If this post helps you mark it as answer
prasanna shahPosted Nov 30, 2011, 12:27 PM
But i had a problem in c# not asp . It's quite different but thanks any way
thanks
prasanna
Satyapriya NayakPosted Nov 30, 2011, 10:30 AM
Here is you solution....
Run the attachments.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add("Select");
DropDownList1.Items.Add("Important");
DropDownList1.Items.Add("very Important");
}
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "insert into Details(ID,Name,Location,flag) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + DropDownList1.SelectedItem.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
bindgrid();
}
void bindgrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from Details";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "Details");
GridView1.DataSource = ds;
GridView1.DataMember = "Details";
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "flag")) == "Important")
{
e.Row.BackColor = System.Drawing.Color.Blue;
}
else
{
e.Row.ForeColor = System.Drawing.Color.Red;
}
}
}
}
Thanks
If this post helps you mark it as answer