Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > see this datagrid problem

Reply
Thread Tools

see this datagrid problem

 
 
a developer
Guest
Posts: n/a
 
      11-13-2004
my table has total 3 columns like emp_id(primay key),emp_name emp_address
i m populating my datagrid at run time successfully .
now i add functionality of hotmail model(checkboxex) in it successfully
but for this i did enhanced simple datagrid's functionality and
add one template column and placed label on it (so that i could do deletion
of records
selcted by user by checkboxes when a user clik on "Delete" button .
in label i took emp_id as field

NOW MY PROBLEM IS

"how can i hide my emp_id column at run time ..i need(want)
to see only 3 columns at run time(checkbox templte column
,emp_name,emp_address)
i tried DataGrid1.Columns[2].Visible=false(2 is number of emp_id)
but it did give following error..


Exception Details: System.ArgumentOutOfRangeException: Index was out of
range.
Must be non-negative and less than the size of the collection. Parameter
name: index
DataGrid1.Columns[2].Visible=false;<----------------(error in this
line)
if i dont choose emp_id in SQL Query then a new error comes in my template
column.."Unknown Column name"
plz sugesst me
ACTUAL CODE IS :-
-------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace fifthpro
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnAdd;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public SqlConnection objConn;
public const string ConnStr=
"uid=sa;pwd=password;server=NEW103;database=Car_te st";

protected System.Web.UI.WebControls.Button Confirm;


private void Page_Load(object sender, System.EventArgs e)
{

// Put user code to initialize the page here
if(!IsPostBack)
{
fnFillGrid();
}


string jsScript = "<script language=JavaScript> "
+" function confirmDelete (frm) {"
+" for (i=0; i<frm.length; i++) {"
+" if (frm.elements[i].name.indexOf ('DeleteThis') !=-1) {"
+" if(frm.elements[i].checked) {return confirm ('Are you sure you want
to delete your selection(s)?')}"
+" }}}"
+" function select_deselectAll (chkVal, idVal){"
+" var frm = document.forms[0];"
+" for (i=0; i<frm.length; i++) {"
+" if (idVal.indexOf ('CheckAll') != -1) {"
+" if(chkVal == true) { frm.elements[i].checked = true;"
+" } else {"
+" frm.elements[i].checked = false;"
+" } } else if (idVal.indexOf('DeleteThis') != -1) {"
+" if(frm.elements[i].checked == false) {"
+" frm.elements[1].checked = false;"
+" } }}}</script>";

RegisterClientScriptBlock("clientScript", jsScript);
WebControl button = (WebControl) Page.FindControl ("Confirm");
button.Attributes.Add ("onclick", "return confirmDelete (this.form);");
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
this.DataGrid1.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEvent Handler(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


#region "UI Layer"

private void btnAdd_Click(object sender, System.EventArgs e)
{
string strCheckStatus="Add";
Session["strCheckStatus"]=strCheckStatus;
Response.Redirect("SetAlert.aspx");
}

private void fnFillGrid()
{
DataSet ds=new DataSet();
ds=fnFillControls();
DataGrid1.DataSource=ds;
DataGrid1.Columns[2].Visible=false;

DataGrid1.DataBind();

foreach (DataGridItem dgi in DataGrid1.Items)
{
dgi.Attributes.Add("onmouseover", "this.style.backgroundColor='#ffcc99'");
dgi.Attributes.Add("onmouseout",
"this.style.backgroundColor='Peachpuff'");
}
}

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
fnFillGrid();
}
#endregion

#region "Business Logic"

public DataSet fnFillControls()
{
//string SqlStatement="select
AlertID,Date,Site,ComparesWith,CountryID,CityId,Ca rType,Figure,Variance,Compare,EmailId,CcEmailId,BC cEmailId,'<a
target=display href=SetAlert.aspx?ALERTID'+'='+convert(varchar(10 ),ALERTID)+'
>'+convert(varchar(10),SITE)+'</a>' from SetAlert";

string SqlStatement="select Date,Site,CountryId As 'Country',CityId as
'City',CarType,"
+" (figure + ',' + variance+ ',' + compare) as '
Values[Figure,Variance,Compare]',EmailId as 'Emails',"
+" '<a target=display
href=SetAlert.aspx?ALERTID'+'='+convert(varchar(10 ),ALERTID)+'"
+" >'+'<b>DETAILS</b>'+'</a>' AS [Show Detail],AlertID from SetAlert";
return fnGetDataSet(SqlStatement);
}

public void DeleteAlerts (Object sender, EventArgs e)
{
string dgIDs = "";
bool BxsChkd = false;
foreach (DataGridItem i in DataGrid1.Items)
{
CheckBox deleteChkBxItem = (CheckBox)i.FindControl("DeleteThis");
if (deleteChkBxItem.Checked)
{
BxsChkd = true;
dgIDs += Convert.ToInt32(((Label) i.FindControl
("AlertID")).Text.ToString()) + ",";
}
}
string SqlStatement="delete from SetAlert where AlertID IN (" +
dgIDs.Substring(0,dgIDs.LastIndexOf(",")) + ")";
if (BxsChkd == true)
{
SqlConnection objConn1;
const string
ConnStr1="uid=sa;pwd=password;server=NEW103;databa se=CAR_TEST";
objConn1=new SqlConnection(ConnStr1);
string SqlStatement1="delete from SetAlert where AlertID IN (" +
dgIDs.Substring(0,dgIDs.LastIndexOf(",")) + ")";
if(objConn1.State==ConnectionState.Closed)
objConn1.Open();
SqlCommand cmd=new SqlCommand(SqlStatement1,objConn1);
cmd.ExecuteNonQuery();
objConn1.Close();
fnFillGrid();
}
}
#endregion

#region "DataLayer"

private SqlConnection fnGetConnection()
{
objConn=new SqlConnection(ConnStr);
return objConn;
}

private string fnConnectString()
{
string ConnectString="uid=sa;pwd=password;server=NEW103;d atabase=Car_test";
return ConnectString;
}

public DataSet fnGetDataSet(string SqlStatement)
{
DataSet MyDataSet=new DataSet();
objConn=new SqlConnection(fnConnectString());
SqlDataAdapter da=new SqlDataAdapter(SqlStatement,objConn);
da.Fill(MyDataSet);
return MyDataSet;
}

public int fnExecuteNonQuery(string SqlStatement)
{
SqlConnection objConn=fnGetConnection();
if (objConn.State==ConnectionState.Closed)
objConn.Open();
SqlCommand cmd=new SqlCommand(SqlStatement,objConn);
int i=cmd.ExecuteNonQuery();
if (objConn.State==ConnectionState.Open)
objConn.Close();
return i;
}

#endregion

}
}
------------------------------------------------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Desktop can see Laptop, but Laptop can't see Desktop! Plomaris Wireless Networking 3 02-17-2006 01:41 PM
computer freezing-can't see replies-please see inside Fourdogs Computer Support 1 12-06-2004 05:12 AM
Dell Inprion 1100 see's network with air card and regular network card but does not see the card help! Paul Tutle Sr. - OCC Computer Support 1 10-01-2004 09:23 PM
Can you see why I'm getting a NoClassDefFoundError here? I can't see it :< Flip Java 3 02-09-2004 10:13 PM
INTERESTING PROBLEM -- CD doesn not see burned disks, but does see others md Computer Support 8 09-03-2003 09:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57