Click here to Skip to main content
16,017,333 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
My code below is working fine. I have three column in my table( TypeID,Description,Rating). As of now all is populated in my datagrid.

But what I need is My datagrid should pull only 2 coulmn from my table( TypeID and Description). How to alter the code?

Please help with code.
Thanks.

My code:
C#
namespace MovieProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void AutoradioButton1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Server=CHRIS-PC\\SQLEXPRESS;Database=MovieDesc;" +"Integrated Security=True");

            SqlCommand scmd = new SqlCommand("SELECT * FROM MovieType", conn);
            DataSet sds = new DataSet();
            conn.Open();

            SqlDataAdapter sadptr = new SqlDataAdapter(scmd);
            sadptr.Fill(sds, "MovieType");

            if (sds.Tables["MovieType"].Rows.Count == 0)
            {
                MessageBox.Show("There are no DB.");
            }
            else
            {
                this.listBox1.DataSource = sds.Tables["MovieType"];
                this.listBox1.DisplayMember = "Description";
            }

            checkedListBox1.DataSource = sds.Tables["MovieType"];
            checkedListBox1.DisplayMember = "Rating";
            dataGridView1.DataSource = sds.Tables[0];
          }
        }
    }
Posted
Updated 30-Sep-10 2:28am
v2

The SQL "Select" statement uses the wildcard, * and so returns all columns in the table.

Change your SQL SELECT statement to select exactly the columns you want.

so

SELECT * FROM MovieType

becomes:

SELECT TypeID, Description FROM MovieType

You also need to change your display member from "Rating" to one of the columns that is being returned.

That should be it.
 
Share this answer
 
Comments
Vista007 30-Sep-10 3:09am    
As from code i am also populating some colum into listbox and checked list box. So SELECT * FROM MovieType is correct for that.
To populate only 2 coulmn form a table of 3 column. code below is not working.

this.dataGridView1.DataSource = sds.Tables[#Quote#MovieType#Quote#];
this.dataGridView1.DisplayMember = #Quote#Rating#Quote#,#Quote#TypeID#Quote#;

Please help. also Datagrid doesnt have property of DisplayMember .

Please help
Try this one
DataTable dt1 = sds.Tables[0];
dt1.Columns.Remove(dt1.Columns[0]);
dt1.AcceptChanges();
dataGridView1.DataSource = dt1;
 
Share this answer
 
Comments
Vista007 1-Oct-10 4:21am    
DataTable dt1 = sds.Tables[0];
dt1.Columns.Remove(dt1.Columns[2]);
dt1.AcceptChanges();
dataGridView1.DataSource = dt1;

hi, The above code removes the column which i want to remove from data grid. but simultaneously tha checkedlistbox populating the column 3 value is now showing "System.data.datasource " in all coulmn in checklist box?

how now?
AshiqueAhammed 1-Oct-10 6:22am    
I think the better way to do it is
dataGridView1.Columns[0].Visible = false;
or build your DataGrid yourself, select all and then show only the columns you want, best practice to do the above though really, less data pulled = less overload etc etc
 
Share this answer
 

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