The problem is you have two different itemDelimiters. Your field "alllines" has tab delimited data while your gSelectedCategories has comma delimited. Try:
global gAllLines,gSelectedCategories,gMyCategories
on mouseUp
put empty into gAllLines
put fld "alllines" of cd "settings_files" of stack "settingsandfiles" into gAllLines
put empty into gMyCategories
replace comma with tab in gSelectedCategories
set the itemDelimiter to tab
repeat for each line i in gAllLines
if item 2 of i is among the items of gSelectedCategories then put i & cr after gMyCategories
end repeat
set the dgText of group "mycategories_compact" to gMyCategories
end mouseUp
Edit
I never use dgText so I'm not sure why buy this datagrid seems to not accept dgText["firstLineContainsColumnNames"] any more. So to me the logical solution is to use dgData:
global gAllLines,gSelectedCategories,gMyCategories
on mouseUp
set the dgData of group "mycategories_compact" to empty
put empty into gMyCategories
replace comma with tab in gSelectedCategories
set the itemDelimiter to tab
local tIndex = 1,tDataA
repeat for each line i in gAllLines
if item 2 of i is among the items of gSelectedCategories then
put item 1 of i into tDataA[tIndex]["Text"]
put item 2 of i into tDataA[tIndex]["Category"]
add 1 to tIndex
end if
end repeat
set the dgData of group "mycategories_compact" to tDataA
end mouseUp