May 24, 2018

Remove empty rows and cells in DataGridView C#

int rowCount = ExceldataGridView.RowCount;
int cellCount = ExceldataGridView.ColumnCount;
bool isNotRemoveRow = false;
bool isNotRemoveCell = false;

try
{
for (int i = 0; i < cellCount; i++)
{
for (int j = 0; j < rowCount; j++)
{
var rowValue = ExceldataGridView.Rows[j].Cells[i].Value.ToString();
if (rowValue != "")
{
isNotRemoveCell = true;
}
}

if (isNotRemoveCell == false)
{
ExceldataGridView.Columns.RemoveAt(i);
cellCount--;
i--;
}

isNotRemoveCell = false;
}

for (int k = 0; k < rowCount; k++)
{
for (int o = 0; o < cellCount; o++)
{
var cellValue = ExceldataGridView.Rows[k].Cells[o].Value.ToString();
if (cellValue != "")
{
isNotRemoveRow = true;
}
}

if (isNotRemoveRow == false)
{
ExceldataGridView.Rows.RemoveAt(k);
rowCount--;
k--;
}

isNotRemoveRow = false;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}

No comments:

Post a Comment