Alex Gordon
Alex Gordon

Reputation: 60741

c# converting from System.Data.DataRowView to string

i have a combobox whose datasource is a datatable. i need to loop through the items in the combobox, but how would i do this? i need to be able to convert each object of type 'System.Data.DataRowView' to string. any advice greatly appreciated!@

Upvotes: 1

Views: 14698

Answers (5)

BenPace
BenPace

Reputation: 1

Hope this helps someone some day. Took me a while to come up with a solution for making a checkedboxlist populate from SQL server.

public partial class MemberSearch : Form
{
    List<string> DataList = new List<string>(1);

    public void GetTypesTable()
    {
        var CONEX = Properties.Settings.Default.Server_1_Conn;
        var sql = "SELECT Type_List FROM List_Member_Types;";
        DataTable dt = new DataTable();

        using (SqlConnection c = new SqlConnection(CONEX))
        using (SqlDataAdapter sda = new SqlDataAdapter(sql, c))
        
            sda.Fill(dt);
            DataView view = new DataView(dt);
            DataTable s = dt;

        foreach (DataRow ROW in s.Rows)

            DataList.Add(ROW[0].ToString());
            ckLstBox_MemTypes.DataSource = DataList;

    }
}

Upvotes: 0

Sameer Bahad
Sameer Bahad

Reputation: 583

Solution:

DataTable DtShow=new DataTable();

for (int i = 0; i < DtShow.Rows.Count; i++)

 {

   Console.WriteLine(DtShow.Rows[i].Field<string>(0));

 }

**   Field<string>(0) Column number start from 0

Upvotes: 0

genericuser
genericuser

Reputation: 1450

Workaround for the same, hope it helps: Convert the dataview source back to datatable and then loop through it.

DataView dt = (DataView)comboBox1.DataSource;
        DataTable s = dt.Table;
        foreach(DataRow dr in s.Rows)

            MessageBox.Show(dr[0].ToString()); 

Upvotes: 1

Jeff Ogata
Jeff Ogata

Reputation: 57783

Based on your recent questions, it sounds like you are trying to figure out how to find or set the selected item in the combobox based on the text displayed in the item. I am not exactly sure how you have things set up, but please look at the following code and see if it helps:

private void button1_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("Name", typeof(string));

    dt.Rows.Add(1, "A");
    dt.Rows.Add(2, "B");
    dt.Rows.Add(3, "C");

    comboBox1.DataSource = dt;
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember = "ID";

    // use SelectedValue to select the item with ID == 2
    comboBox1.SelectedValue = 2;

    // use FindStringExact() to find the index of text displayed in the item
    comboBox1.SelectedIndex = comboBox1.FindStringExact("C");
}

and using a combobox as set up above, you can get the text of the display member like this:

private void button2_Click(object sender, EventArgs e)
{
    foreach (var item in comboBox1.Items)
        MessageBox.Show(((DataRowView)item)["Name"].ToString());
}

Upvotes: 4

David Archer
David Archer

Reputation: 2028

Well... to loop through a combobox, use (slightly pseudocoding, please don't c+p without working on the code):

var newItems = new List<string>();
for(var i = 0; i < combobox1.Items.Count; i++)
{
   newItems.Add(combobox1.items[i].Text);
}

Then to access each item, use:

foreach(item in newItems)
{
var newVariable1 = item;
}

More info and your current code would be cool, I'd be able to help you more specifically with your problem that way.

Upvotes: 0

Related Questions