Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to map a datagridview column to a column of the datasource datatable? [CS/C# 2005

Started by Shaitan00 Oct 1, 2006 at 8:36 PM 0 replies 28k views
Original Post
Shaitan00
Shaitan00
Given a DataGridView (dgvPlayerList) used to display all the players and their current information to the user. Given a DataSet (dsPlayerList.Tables["PlayerList"]) that house the list of players and their information to be displayed as the datasource of the datagridview (dgvPlayerList)... The DataTable (PlayerList) is composed of 4 columns: [ID], [Name], [Type], [IP]. Currently I am using the following code to display the information: dgvPlayerList.DataSource = dsPlayerList.Tables["PlayerList"]; Problem is this displays all 4 columns and I do not want to shown the [ID] or [IP] columns in the datagridview - so I tried to customize the coloumns collection by creating the [Name] and [Type] columns and setting "dgvPlayerList.AutoGenerateColumns = false;" but when I try the columns are never filled with data ... It seems I need a way to map the datagridview column with its corresponding column in the DataSet.Tables["PlayerList"], I did some reading and people mentioned the DataMember & ValueMember field but little to no examples where provided and I couldn't get it to work. Others spoke of the DataPropertyName but again little to no examples or samples... So I was hopeing someone could help shed some light on my little problem - I just want to MAP (somehow) my columns in the datagridview column collection with a specific column in the datasource datatable column... Any ideas, hints, and help would be greatly appreciated, thanks
daerid
daerid
Quick test in VS.net 2005, using the following code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace TestWinApp{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            DataSet dsPlayerList = new DataSet();            DataTable dt = new DataTable("PlayerList");            dsPlayerList.Tables.Add(dt);            dt.Columns.Add("ID",typeof(int));            dt.Columns.Add("Name",typeof(string));            dt.Columns.Add("Type", typeof(string));            dt.Columns.Add("IP", typeof(string));            DataRow dr = dt.NewRow();            dr["ID"] = 1;            dr["Name"] = "Player 1";            dr["Type"] = "Standard";            dr["IP"] = "127.0.0.1";            dt.Rows.Add(dr);            dr = dt.NewRow();            dr["ID"] = 2;            dr["Name"] = "Player 2";            dr["Type"] = "Standard";            dr["IP"] = "127.0.0.1";            dt.Rows.Add(dr);            dt.AcceptChanges();            this.dgvPlayerList.AutoGenerateColumns = false;            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();            col.DataPropertyName = "Name";            col.Name = "Name";            col.HeaderText = "Name";                        this.dgvPlayerList.Columns.Add(col);            col = new DataGridViewTextBoxColumn();            col.DataPropertyName = "Type";            col.Name = "Type";            col.HeaderText = "Type";            this.dgvPlayerList.Columns.Add(col);            this.dgvPlayerList.DataSource = dsPlayerList.Tables["PlayerList"];                    }    }}
daerid@gmail.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.