Click here to Skip to main content
15,065,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
How to hide the id column ? datagrid in C # or just show one column
thank you in advance for your help
cordially 

C#
private void Grid_Click(object sender, EventArgs e)
{

    i = Convert.ToInt32(DT.Rows[Grid.CurrentRowIndex]["id"]);
    btnDel.Enabled = true;
    btnEdit.Enabled = true;
    txtData1.Text = DT.Rows[Grid.CurrentRowIndex]["data1"].ToString();
    txtData2.Text = DT.Rows[Grid.CurrentRowIndex]["data2"].ToString();

}


What I have tried:

<pre lang="C#">

		private void Grid_Click(object sender, EventArgs e)
		{

            i = Convert.ToInt32(DT.Rows[Grid.CurrentRowIndex]["id"]);
		    btnDel.Enabled = true;
			btnEdit.Enabled = true;
			txtData1.Text = DT.Rows[Grid.CurrentRowIndex]["data1"].ToString();
            txtData2.Text = DT.Rows[Grid.CurrentRowIndex]["data2"].ToString();
            
        }
Posted
Updated 31-Aug-21 4:30am

In a data grid you define the columns you want to show. If the data source, a data table for example, contains a column you don't want to show then don't create a column for it.

For example if you're using WPF, have a look at DataGrid.Columns Property (System.Windows.Controls) | Microsoft Docs[^]

Looking at the properties you use it seems that you're using Forms DataGrid. If this is true, I would recommend using DataGridView instead since it has replaced DataGrid. Have a look at Differences Between DataGridView and DataGrid Controls - Windows Forms .NET Framework | Microsoft Docs[^]

With DataGridView you would use DataGridView.Columns Property (System.Windows.Forms) | Microsoft Docs[^]
   
v2
Hi,
1. If you have a boundField in grid view you can use
 <HeaderStyle CssClass="hidden" Width="30%" />
<ItemStyle CssClass="hidden" Width="30%" Wrap="False" />


2.if you want without boundField

void Grid_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
   e.Row.Cells[0].Visible = false;
}


3.if you are using TemplateField

<asp:TemplateField HeaderText="Customer Name" Visible="false">
    <ItemTemplate>
        <asp:Label Text='<%# Eval("CustomerName") %>' runat="server" ID="LblCusomerName" />
    </ItemTemplate>
</asp:TemplateField>


I hope this will help you.
   
v3
Hello,

Thank you for faster solution, it's run great now.

Best regards.
   
Comments
Richard Deeming 31-Aug-21 10:53am
   
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as a solution.
Member 15340081 31-Aug-21 17:58pm
   
Hello,

Thank you for faster solution, it's run great now.

Best regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900