datagridview med combobox
Jeg har "lidt" problemer med en datagridview med en combobox i.Jeg kan simpel hen ikke få data'en ud af den eller sætte et index/value i combobox'en.
Det er dog lykkes mig at lave selve combobox'en men den data i som der skal være.
En der kan forklare mig hvordan jeg f.eks. kan få data'en ud af
dataGridView1, row 0, collone 3 (som er en combobox).
Eller hvordan jeg kan sætte den?
Her er min kode so far!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Indexer;
using SeriesIdentifier;
using SerieInfo;
using System.IO;
using System.Threading;
using System.Collections;
using System.Data.SqlClient;
namespace Seriesindexer
{
public partial class SelectImdb : Form
{
public SelectImdb()
{
InitializeComponent();
}
private DatabaseDataSetTableAdapters.FilesTableAdapter TAFile = new Seriesindexer.DatabaseDataSetTableAdapters.FilesTableAdapter();
private DatabaseDataSetTableAdapters.FoldersTableAdapter TAFolder = new Seriesindexer.DatabaseDataSetTableAdapters.FoldersTableAdapter();
SerieSearch Search = new SerieSearch();
DataTable _DataTable;
DataRow dr;
private void SelectImdb_Load(object sender, EventArgs e)
{
DataTable Shows = Populate("SELECT DISTINCT serie FROM Files");
for (int i = 0; i < Shows.Rows.Count && i <= 2; i++)
{
ResetDataTable();
if (Shows.Rows[i]["serie"].ToString() != "")
{
ArrayList Serie = Search.GetImdbSelect(Shows.Rows[i]["serie"].ToString());
foreach (string[] s in Serie)
{
dr = this._DataTable.NewRow();
dr["text"] = s[0];
dr["value"] = s[1];
this._DataTable.Rows.Add(dr);
}
DataGridViewRow heterow = new DataGridViewRow();
DataGridViewComboBoxCell comboCell = new DataGridViewComboBoxCell();
comboCell.DataSource = _DataTable;
comboCell.ValueMember = "value";
comboCell.DisplayMember = "text";
DataGridViewCheckBoxCell checkbox = new DataGridViewCheckBoxCell();
checkbox.Value = true;
DataGridViewTextBoxCell FileName = new DataGridViewTextBoxCell();
FileName.Value = Shows.Rows[i]["serie"].ToString();
DataGridViewTextBoxCell imdb = new DataGridViewTextBoxCell();
imdb.Value = "";
heterow.Cells.Add(checkbox);
heterow.Cells.Add(FileName);
heterow.Cells.Add(comboCell);
heterow.Cells.Add(imdb);
dataGridView.Rows.Add(heterow);
}
}
}
private void ResetDataTable()
{
_DataTable = new DataTable();
DataColumn dc;
dc = new DataColumn("text", System.Type.GetType("System.String"));
this._DataTable.Columns.Add(dc);
dc = new DataColumn("value", System.Type.GetType("System.String"));
this._DataTable.Columns.Add(dc);
dr = this._DataTable.NewRow();
dr["text"] = "Custom";
dr["value"] = "";
this._DataTable.Rows.Add(dr);
}
private void buttonFinished_Click(object sender, EventArgs e)
{
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string txt = ((ComboBox)sender).SelectedValue.ToString();
int rowIndex = this.dataGridView.CurrentRow.Index;
DataGridViewRow row = this.dataGridView.Rows[rowIndex];
this.dataGridView[3, rowIndex].Value = string.Format("{0}", txt);
}
catch
{ }
}
private void dataGridView_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView.CurrentCell.ColumnIndex == 3)
{
// If we are editing the imdb link
// ARGH set combobox til custom, for crying out loud!!
}
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(ComboBox_SelectedIndexChanged);
// Add the event handler.
combo.SelectedIndexChanged +=
new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private DataTable Populate(string sqlCommand)
{
SqlConnection northwindConnection = new SqlConnection(global::Seriesindexer.Properties.Settings.Default.DatabaseConnectionString);
northwindConnection.Open();
SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
return table;
}
}
}