Gridview in ASP.Net with C# [modified]
-
Below is my databse table.. I can populate this data into the UI Page.. My task is to automatically delete the columns which has '0'(zero) value...and i have to delete the first row of my table... Hear i am not going to use any itemtemplate column.. I have to write some code in codebehind..
JurisdictionID JurisdictionDescription Year1 Year2 Year3 Year4 Year5 Year6 Year7
2005 2006 2009 0 0 0 0
AL Alabama 0 0 6 0 0 0 0
AK Alaska 2 0 0 0 0 0 0Could Anyone suggest me ..How to proceed further in detail with some code...:confused:
modified on Monday, June 1, 2009 4:24 PM
-
Below is my databse table.. I can populate this data into the UI Page.. My task is to automatically delete the columns which has '0'(zero) value...and i have to delete the first row of my table... Hear i am not going to use any itemtemplate column.. I have to write some code in codebehind..
JurisdictionID JurisdictionDescription Year1 Year2 Year3 Year4 Year5 Year6 Year7
2005 2006 2009 0 0 0 0
AL Alabama 0 0 6 0 0 0 0
AK Alaska 2 0 0 0 0 0 0Could Anyone suggest me ..How to proceed further in detail with some code...:confused:
modified on Monday, June 1, 2009 4:24 PM
foreach (GridViewRow Row in grd.Rows) { for (int i = 0; i<7; i++) { if (Row[i].ToString().Equals("0")) { grd.Columns.Remove(Row[i]); } } } I have not tested this code. Let me know if it works or not so as to fix the errors. Regards
-
foreach (GridViewRow Row in grd.Rows) { for (int i = 0; i<7; i++) { if (Row[i].ToString().Equals("0")) { grd.Columns.Remove(Row[i]); } } } I have not tested this code. Let me know if it works or not so as to fix the errors. Regards
hey..i am getting some errors..when am using that code.. in which method we have to write tht code.. i wrote in...
public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
i am getting errors like this.. 1) 'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level 2) Cannot apply indexing with [] to an expression of 'System.Web.UI.WebControls.GridViewRow' actually am poor in c# technologies..could you sen dme solution fot this..:confused:
-
hey..i am getting some errors..when am using that code.. in which method we have to write tht code.. i wrote in...
public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
i am getting errors like this.. 1) 'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level 2) Cannot apply indexing with [] to an expression of 'System.Web.UI.WebControls.GridViewRow' actually am poor in c# technologies..could you sen dme solution fot this..:confused:
Sorry, I was closing from work when I posted. This should work: foreach (GridViewRow Row in GridView1.Rows) { for(int i=0; i < 7; i++) { if (Row.Cells[i].Text == "0") { GridView1.Columns.Remove(GridView1.Columns[i]); } } } I called up this function from a button which I created. You can place it within the GridView1_RowDeleting if you wish without problems. As per deleting first row, I suggest deleting it from database first (using sql query) and re-binding your gridview.
-
Sorry, I was closing from work when I posted. This should work: foreach (GridViewRow Row in GridView1.Rows) { for(int i=0; i < 7; i++) { if (Row.Cells[i].Text == "0") { GridView1.Columns.Remove(GridView1.Columns[i]); } } } I called up this function from a button which I created. You can place it within the GridView1_RowDeleting if you wish without problems. As per deleting first row, I suggest deleting it from database first (using sql query) and re-binding your gridview.
hey..thnx for ur reply.. i am writting the code in the row delete event only... i am not getting any errors..when i execute this code...somehow am unable to delete the columns.... is there any pother way to do..?
-
hey..thnx for ur reply.. i am writting the code in the row delete event only... i am not getting any errors..when i execute this code...somehow am unable to delete the columns.... is there any pother way to do..?
If what you want to do is to delete a column from the underlying database table I'm not sure I can help you on that. If what you want h/ever is to simply hide the column from the GridView this should work otherwise you can replace GridView1.Columns.Remove(GridView1.Columns[i]); with GridView1.Column[i].Visible = false; I've tested the previous code and it worked. Why not post more code for us to see where you're getting it wrong?
-
If what you want to do is to delete a column from the underlying database table I'm not sure I can help you on that. If what you want h/ever is to simply hide the column from the GridView this should work otherwise you can replace GridView1.Columns.Remove(GridView1.Columns[i]); with GridView1.Column[i].Visible = false; I've tested the previous code and it worked. Why not post more code for us to see where you're getting it wrong?
by using above syntax also..i can't see anychange in the table..i can see all the columns..the 0(zero) value cell columns are not hiding..:( actullt my task is to delete those columns..am unable to do that.. atleast i want to try with hiding those columns.. :(( below i copied my code of populating data into gridview and the rowdelete event..
protected void Page_Load(object sender, EventArgs e)
{
PopulateGrid();} public void PopulateGrid() { SqlConnection sqlConn = null; SqlDataAdapter da = null; try { string connString = null; SqlCommand cmd1; connString = System.Web.Configuration.WebConfigurationManager.AppSettings\["SqlServerConnectionString"\]; //connString = "Data Source = SQD-CON4\\QSRV1; Initial Catalog = NDI; Integrated Security = SSPI;"; sqlConn = new SqlConnection(connString); sqlConn.Open(); cmd1 = new SqlCommand("\[MED\].\[usp\_SelectAllSDNCounts\]", sqlConn); cmd1.CommandType = CommandType.StoredProcedure; da = new SqlDataAdapter(cmd1); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds.Tables\[0\].DefaultView; GridView1.DataBind(); } finally { sqlConn.Close(); da.Dispose(); da = null; } }
public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{foreach (GridViewRow Row in GridView1.Rows) { for(int i=0; i<11; i++) { if (Row.Cells\[i\].Text == "0") { GridView1.Columns\[i\].Visible=false; } } } }
-
by using above syntax also..i can't see anychange in the table..i can see all the columns..the 0(zero) value cell columns are not hiding..:( actullt my task is to delete those columns..am unable to do that.. atleast i want to try with hiding those columns.. :(( below i copied my code of populating data into gridview and the rowdelete event..
protected void Page_Load(object sender, EventArgs e)
{
PopulateGrid();} public void PopulateGrid() { SqlConnection sqlConn = null; SqlDataAdapter da = null; try { string connString = null; SqlCommand cmd1; connString = System.Web.Configuration.WebConfigurationManager.AppSettings\["SqlServerConnectionString"\]; //connString = "Data Source = SQD-CON4\\QSRV1; Initial Catalog = NDI; Integrated Security = SSPI;"; sqlConn = new SqlConnection(connString); sqlConn.Open(); cmd1 = new SqlCommand("\[MED\].\[usp\_SelectAllSDNCounts\]", sqlConn); cmd1.CommandType = CommandType.StoredProcedure; da = new SqlDataAdapter(cmd1); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds.Tables\[0\].DefaultView; GridView1.DataBind(); } finally { sqlConn.Close(); da.Dispose(); da = null; } }
public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{foreach (GridViewRow Row in GridView1.Rows) { for(int i=0; i<11; i++) { if (Row.Cells\[i\].Text == "0") { GridView1.Columns\[i\].Visible=false; } } } }
Change: protected void Page_Load(object sender, EventArgs e) { PopulateGrid(); } to: protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { PopulateGrid(); } } Does this help? :)
-
Change: protected void Page_Load(object sender, EventArgs e) { PopulateGrid(); } to: protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { PopulateGrid(); } } Does this help? :)
no..hmmm:( it remains the same..i can see the table with all column .. if u want to know my design page ..see below..
%@ Page Language="C#" AutoEventWireup="true" CodeFile="MedPending.aspx.cs" Inherits="Maintenance_MedPending" %>
Untitled Page
:confused:
-
no..hmmm:( it remains the same..i can see the table with all column .. if u want to know my design page ..see below..
%@ Page Language="C#" AutoEventWireup="true" CodeFile="MedPending.aspx.cs" Inherits="Maintenance_MedPending" %>
Untitled Page
:confused:
1. Try putting a breakpoint in the code provided and see if the Row's Cell Text ever evaluates to "0". Does it ever enter the function i gave you? 2. Have you hooked the Row_Deleting method to the control calling it? If not add: OnRowDeleting = "GridView1_RowDeleting" to this: OnRowDeleting = "GridView1_RowDeleting" CssClass="GridRowGrayBorder"> Or alternatively in Page_Load add: GridView1.RowDeleting+=new GridViewDeleteEventHandler(GridView1_RowDeleting); Hope this helps
-
no..hmmm:( it remains the same..i can see the table with all column .. if u want to know my design page ..see below..
%@ Page Language="C#" AutoEventWireup="true" CodeFile="MedPending.aspx.cs" Inherits="Maintenance_MedPending" %>
Untitled Page
:confused:
If this suggestion helped you, kindly say so, so the thread will be marked as resolved.