Prabhat 7154it

Prabhat 7154it

  • NA
  • 61
  • 7.9k

Hide specific numbers of columns in DataGridView

Dec 2 2019 12:10 AM
I want to hide few columns named "id","image", "slug" and "updated_at" in my DataGridView.But I want to use column named "id" for updation and deletion.So How can I do so ? Please make change in loadPage() method. 
 
================================================================================ 
 
I have a table named "lwpos_categories" whose table definition is 
CREATE TABLE [dbo].[lwpos_categories]
(
[id] INT IDENTITY (1, 1) NOT NULL,
[code] VARCHAR (55) NOT NULL,
[name] VARCHAR (55) NOT NULL,
[image] VARCHAR (55) DEFAULT (NULL) NULL,
[parent_id] INT DEFAULT (NULL) NULL,
[slug] VARCHAR (55) DEFAULT (NULL) NULL,
[updated_at] DATETIME2 (0) DEFAULT (getdate()) NOT NULL,
PRIMARY KEY CLUSTERED ([id] ASC)
);
 
=============================================================================
 
private void loadPage()
{
int intSkip = 0, i = 0;
intSkip = (this.currentPage * this.pageSize);
string query = "SELECT * FROM lwpos_categories";
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("lwpos_categories");
// Add the table columns.
for (i = 0; i < sdr.FieldCount; i++)
{
dt.Columns.Add(sdr.GetName(i), sdr.GetFieldType(i));
}
int intIdx = 0;
while (sdr.Read())
{
if (intIdx >= intSkip)
{
DataRow row = dt.NewRow();
// Assign DataReader values to DataRow.
for (i = 0; i < sdr.FieldCount; i++)
{
row[i] = sdr[i];
}
dt.Rows.Add(row);
}
if ((intIdx - intSkip) >= (this.pageSize - 1))
{
break;
}
intIdx++;
}
sdr.Close();
// Populate Data Grid
this.CategoryGV.DataSource = ds.Tables["lwpos_categories"].DefaultView;
// Show Status
this.txtPageStatus.Text = "Showing Page: " + (this.currentPage + 1).ToString() + " of " + this.pageCount.ToString();
cmd.Dispose();
ds.Dispose();
con.Close();
}
 
 ===============================================================================
 
See the image it is showing all the columns which is not desired.The column "id","image","slug" and "updated_at" should not be shown that is these columns should remain hided in DataGridView.
 
 
=========================================++++++++++=============================== 
private int totalRecords = 0;
private int pageSize = 0;
private int pageCount = 0;
private int currentPage = 1;
 
private void fillGrid(int intPageSize)
{
// For Page view.
this.pageSize = intPageSize;
this.totalRecords = getNoOfRecords();
this.pageCount = this.totalRecords / this.pageSize;
// Adjust page count if the last page contains partial page.
if (this.totalRecords % this.pageSize > 0)
{
this.pageCount++;
}
this.currentPage = 0;
loadPage();
}
 

Answers (1)