将多个datagrid单元格保存为多个字符串 [英] Saving multiple datagrid cells to multiple strings

查看:57
本文介绍了将多个datagrid单元格保存为多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格视图,显示来自MySQL数据库的信息。当我双击单元格时,我已将所有内容从gridview保存为公共字符串,以另一种形式使用(当我双击时打开),所以我不必保持MySQL连接打开以再次检索信息。 。



我这样做的方法是:



I have a datagrid view that shows information from a MySQL database. When I double click on the cell I have saved everything from the gridview as public strings to be used in another form (that opens when I double click) so I don't have to keep the MySQL connection open to retrieve the information again...

My approach for doing this is:

private void grid_laptops_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        DataGridViewRow laprow = this.grid_laptops.Rows[e.RowIndex];

        Public_Variables.laptop_id = laprow.Cells[0].Value.ToString();
        Public_Variables.laptop_stockid = laprow.Cells[1].Value.ToString();
        Public_Variables.laptop_manufacture = laprow.Cells[2].Value.ToString();
        Public_Variables.laptop_model = laprow.Cells[3].Value.ToString();
        Public_Variables.laptop_servicetag = laprow.Cells[4].Value.ToString();
        Public_Variables.laptop_condition = laprow.Cells[5].Value.ToString();
        Public_Variables.laptop_cpumodel = laprow.Cells[6].Value.ToString();
        Public_Variables.laptop_cpuspeed = laprow.Cells[7].Value.ToString();
        Public_Variables.laptop_memoryamount = laprow.Cells[8].Value.ToString();
        Public_Variables.laptop_memorytype = laprow.Cells[9].Value.ToString();
        Public_Variables.laptop_harddrive = laprow.Cells[10].Value.ToString();
        Public_Variables.laptop_videocard = laprow.Cells[11].Value.ToString();
        Public_Variables.laptop_videomemory = laprow.Cells[12].Value.ToString();
        Public_Variables.laptop_os = laprow.Cells[13].Value.ToString();
        Public_Variables.laptop_screensize = laprow.Cells[14].Value.ToString();
        Public_Variables.laptop_resolution = laprow.Cells[15].Value.ToString();
        Public_Variables.laptop_webcam = laprow.Cells[16].Value.ToString();
        Public_Variables.laptop_acadapter = laprow.Cells[17].Value.ToString();
        Public_Variables.laptop_ebayid = laprow.Cells[18].Value.ToString();
        Public_Variables.laptop_ebayprice = laprow.Cells[19].Value.ToString();
        Public_Variables.laptop_ebaylink = laprow.Cells[20].Value.ToString();
        Public_Variables.laptop_ebayquantity = laprow.Cells[21].Value.ToString();
        Public_Variables.laptop_ebaystatus = laprow.Cells[22].Value.ToString();
    }

    Single_Window___Grid_View.SingleWindow_Laptop window = new Single_Window___Grid_View.SingleWindow_Laptop();
    window.Show();
}





虽然我认为在分配所有这些变量时可以使用类似的方法以我的其他形式的文本框。我想要一种更清洁的方法来快速抓住所有这些单元格(在同一行),基本概念是不正确的:





Although I think it would be possible to use a similar method when I assign all these variables to text boxes in my other form. I would like a cleaner way to quickly grab all these cells (on the same row), the basic concept that isn't correct:

private void grid_storage_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    string[] storage_public_variables =
    {
        Public_Variables.storage_id, Public_Variables.storage_stockid, Public_Variables.storage_manufacture, Public_Variables.storage_model,
        Public_Variables.storage_servicetag, Public_Variables.storage_condition, Public_Variables.storage_capacity, Public_Variables.storage_firmware,
        Public_Variables.storage_rpm, Public_Variables.storage_cache, Public_Variables.storage_interface, Public_Variables.storage_ebayid,
        Public_Variables.storage_ebayprice, Public_Variables.storage_ebaylink, Public_Variables.storage_ebayquantity, Public_Variables.storage_ebaystatus
    };

    if (e.RowIndex >= 0)
    {
        DataGridViewRow storagerow = this.grid_storage.Rows[e.RowIndex];


        for (int i = 0; i < grid_storage.Rows.Count; i++)
        {
            storage_public_variables[i] = grid_storage.Rows[i].Cells[i].Value.ToString();
        }

        MessageBox.Show("Debug: \n\nStorage ID = " + Public_Variables.storage_id);

    }

    Single_Window___Grid_View.SingleWindow_Storage window = new Single_Window___Grid_View.SingleWindow_Storage();
    window.Show();
}





我尝试过:



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

{

storage_public_variables [i] = grid_storage.Rows [i] .Cells [i] .Value.ToString();

}



还试过:



private void grid_storage_CellMouseDoubleClick(object sender,DataGridViewCellMouseEventArgs e)

{

string [] storage_public_variables =

{

Public_Variables.storage_id,Public_Variables.storage_stockid,Public_Variables.storage_manufacture,Public_Variables.storage_model,

Public_Variables.storage_servicetag,Public_Variables.storage_condition,Public_Variables.storage_capacity,Public_Variables.storage_firmware ,

Public_Variables.storage_rpm,Public_Variables.storage_cache,Public_Var iables.storage_interface,Public_Variables.storage_ebayid,

Public_Variables.storage_ebayprice,Public_Variables.storage_ebaylink,Public_Variables.storage_ebayquantity,Public_Variables.storage_ebaystatus

};



if(e.RowIndex> = 0)

{

DataGridViewRow storagerow = this.grid_storage.Rows [e.RowIndex];



for(int i = 0;我< storage_public_variables.Length; i ++)

{

storage_public_variables [i] = storagerow.Cells [i] .Value.ToString();

}



MessageBox.Show(Debug:\ n\\\
Storage ID =+ Public_Variables.storage_id);



} $ / $


Single_Window ___ Grid_View.SingleWindow_Storage window = new Single_Window ___ Grid_View.SingleWindow_Storage();

window.Show();

}



What I have tried:

for (int i = 0; i < grid_storage.Rows.Count; i++)
{
storage_public_variables[i] = grid_storage.Rows[i].Cells[i].Value.ToString();
}

ALSO tried:

private void grid_storage_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
string[] storage_public_variables =
{
Public_Variables.storage_id, Public_Variables.storage_stockid, Public_Variables.storage_manufacture, Public_Variables.storage_model,
Public_Variables.storage_servicetag, Public_Variables.storage_condition, Public_Variables.storage_capacity, Public_Variables.storage_firmware,
Public_Variables.storage_rpm, Public_Variables.storage_cache, Public_Variables.storage_interface, Public_Variables.storage_ebayid,
Public_Variables.storage_ebayprice, Public_Variables.storage_ebaylink, Public_Variables.storage_ebayquantity, Public_Variables.storage_ebaystatus
};

if (e.RowIndex >= 0)
{
DataGridViewRow storagerow = this.grid_storage.Rows[e.RowIndex];

for (int i = 0; i < storage_public_variables.Length; i++)
{
storage_public_variables[i] = storagerow.Cells[i].Value.ToString();
}

MessageBox.Show("Debug: \n\nStorage ID = " + Public_Variables.storage_id);

}

Single_Window___Grid_View.SingleWindow_Storage window = new Single_Window___Grid_View.SingleWindow_Storage();
window.Show();
}

推荐答案

您应该避免使用全局变量在表单之间传递状态。相反,将状态传递给第二个表单上的方法。



在这种情况下,您只需传递 DataGridViewRow

You should avoid using global variables to pass state between forms. Instead, pass the state to a method on the second form.

In this case, you can simply pass the DataGridViewRow:
// Form 1:
private void grid_storage_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    var window = new Single_Window___Grid_View.SingleWindow_Storage();
    if (e.RowIndex != 0)
    {
        DataGridViewRow storagerow = this.grid_storage.Rows[e.RowIndex];
        window.LoadStorageRow(storagerow);
    }
    
    window.Show();
}


// SingleWindow_Storage:
public void LoadStorageRow(DataGridViewRow storagerow)
{
    if (storagerow == null) return;
    
    txtStorageId.Text = storagerow.Cells[0].Value.ToString();
    txtStockId.Text = storagerow.Cells[1].Value.ToString();
    ...
}


这篇关于将多个datagrid单元格保存为多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆