Click here to Skip to main content
15,887,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a form in c#. the form has two datagridview. The 1st datagridview is retrieving records from database(sql server)
And it also has checkboxes on its 1st column so whatever item was checked, it will be transfered to the 2nd datagridview.i'm using datatable to transfer items to the 2nd datagridview.i also have a textbox used for searching item on the 1st gridview so items will be filtered. There's no problem in adding items on the 2nd gridview but when i search an item and add it on the existing items on the 2nd gridview, it only adds the item that i searched and all the existing items on the 2nd gridview disappeared. i used rowfilter for the search box.pls.help. thank you

What I have tried:

i tried changing the code for searching items on the textbox and remove the RowFilter function of the datatable but it did not work
Posted
Updated 7-Oct-20 19:29pm
v2
Comments
Richard MacCutchan 7-Oct-20 12:13pm    
At a guess there is something wrong in your code. But we cannot see your code so it is impossible to make any suggestions.
rommelpabustan 7-Oct-20 14:20pm    
here's my code for the search textbox:(dgvCustProducts.DataSource as DataTable).DefaultView.RowFilter = string.Format("[ITEM NAME] LIKE '{0}%'", txtSearch.Text);
rommelpabustan 7-Oct-20 14:29pm    
and here's my code for the transfer items:

/passing datagridview records using datatable to another datagridview(Form2)
try
{

if (dgvCustProducts.Rows.Count == 0)
{
MessageBox.Show("No Items to Load.Choose Transaction type first.", "Field Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}
else
{
if (data == null)//solution to object reference not set error for datatable
{
MessageBox.Show("Select first the Items you want to Transfer.", "Field Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

//else if
//{


//}
else
{
data.Columns.Clear();
data.Clear();
//creating columns for datatable
data.Columns.Add("TRANSACTION ID", typeof(string));//0
data.Columns.Add("PRODUCT ID", typeof(string));//1
data.Columns.Add("QTY", typeof(double));//2
data.Columns.Add("UNIT", typeof(string));//3
data.Columns.Add("ARTICLES", typeof(string));//4
data.Columns.Add("UNIT PRICE", typeof(decimal));//5
data.Columns.Add("AMOUNT", typeof(decimal));//6

string tr_prefix = "TR-";
int trans_idno = (new Random()).Next(100, 1000);
transaction = tr_prefix + trans_idno.ToString();
amount = qty * unitprice;

foreach (DataGridViewRow row in dgvCustProducts.Rows)
{

bool isChecked = Convert.ToBoolean(row.Cells["colTransfer"].Value);//original code
if (isChecked)
{


if (dgvCustProducts.SelectedRows.Count >= 1)
{

qty = Convert.ToDouble(row.Cells[1].Value);
if (qty == 0)
{
MessageBox.Show("Please Add a Quantity to the Product you are Transferring", "Quantity", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else
{
qty = Convert.ToDouble(row.Cells[1].Value);
prod_id = row.Cells[2].Value.ToString();
unit = row.Cells[3].Value.ToString();
item_name = row.Cells[4].Value.ToString();
unitprice = Convert.ToDouble(row.Cells[5].Value);
amount = Convert.ToDouble(row.Cells[1].Value) * Convert.ToDouble(row.Cells[5].Value);
data.Rows.Add(transaction, prod_id, row.Cells[1].Value, row.Cells[3].Value, row.Cells[4].Value, row.Cells[5].Value, amount);

}

}


dgvSalesInv.DataSource = data;
double sum = 0;//ililipat lng ng pwesto. dito ilalagay
for (int i = 0; i < dgvSalesInv.Rows.Count; ++i)
{
sum += Convert.ToDouble(dgvSalesInv.Ro

1 do not clear your Columns: create the Columns once, then copy the structure of the first DataTable to the second:
dataGridView1.DataSource = data1;

data2 = data1.Clone(); // copies structure

dataGridView2.DataSource = data2;
2 get the selected Rows in the first DGV, then update the second DataTable:
IEnumerable<DataGridViewRow> selrows = dataGridView1.Rows.Cast<DataGridViewRow>().Where((DataGridViewRow row) =>
Convert.ToBoolean(row.Cells["colTransfer"].Value));

data2.Rows.Clear();

foreach (DataGridViewRow row in selrows)
{
    int ndx = row.Index;
    
    data2.Rows.Add(data1.Rows[ndx].ItemArray);
}
3 I don't see what the problem is with adding additional rows based on searching: get the Index of the new rows, and use the same technique to add them as shown in #2.

Or, maybe, set the CheckBoxes that match the found rows to 'Checked, and then refresh the second DataTable bound to the second DataGridView: clearing the rows, and then using the code in #2,
 
Share this answer
 
use a javascript search it will not refresh the data source.
example code

<script type="text/javascript">
   function Search_GridviewLocation(strKey) {
       var strData = strKey.value.toLowerCase().split(" ");
       var tblData = document.getElementById('<%= GridView4.ClientID %>');
       var rowData;
       for (var i = 1; i < tblData.rows.length; i++) {
           rowData = tblData.rows[i].cells[2].innerText;
           var styleDisplay = 'none';
           for (var j = 0; j < strData.length; j++) {
               if (rowData.toLowerCase().indexOf(strData[j]) >= 0)
                   styleDisplay = '';
               else {
                   styleDisplay = 'none';
                   break;
               }
           }
           tblData.rows[i].style.display = styleDisplay;
       }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900