• Advertisement
Sign in to follow this  

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

This topic is 4132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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"];
}


}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement